Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two tables, and I want to connect the tables by CustomerID (CustomerID is same for two tables). So I used my query like:
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay ON Cus.CustomerID = Pay.CustomerID
WHERE
CustomerID = 2
And it shows an error:
'CustomerID' in where clause is ambiguous
How can I resolve ambiguous column name error in a way that does not add the table name before CustomerID?
Since it doesn't know which CustomerID to use in your where clause CustomerID = 2, you should specify which to use:
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay
ON
Cus.CustomerID = Pay.CustomerID
WHERE
Cus.CustomerID = 2
It doesn't matter if you use Cus.CustomerID or Pay.CustomerID since they will always be the same in your current statement (since you equal them in your join).
The ambigious part is here:
WHERE
CustomerID = 2
Both tables have this column you need to be explict either:
Cus.CustomerID
or
Pay.CustomerID
[I]n a way that does not add the table name before CustomerID
You can use a sub-query.
SELECT CustomerID
FROM
(
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay
ON
Cus.CustomerID = Pay.CustomerID
) query
WHERE CustomerID = 2
Is this a good way? No. This may force the query plan to use a scan instead of a seek if CustomerID is an indexed field (which it should be since this is being joined on).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
Joining 2 tables (orders, addresses)
Orders contains columns delivery_address_id (contains NULL values) and invoice_address_id (does NOT contain NULL values)
Addresses contains id column (does NOT contain NULL values)
Primarily, the LEFT JOIN must be performed on orders.delivery_address_id. However, in the case when its value is NULL in the row, perform LEFT JOIN on orders.invoice_address_id.
How do I deal with this?
I tried the operator OR but the result was not correct. I was also thinking about a CASE WHEN statement. My expectations are to get the LEFT JOIN working.
You can use the operator OR in the ON clause:
SELECT ....
FROM Orders o LEFT JOIN Addresses a
ON a.id = o.delivery_address_id
OR (o.delivery_address_id IS NULL AND a.id = o.invoice_address_id);
Or, use COALESCE():
SELECT ....
FROM Orders o LEFT JOIN Addresses a
ON a.id = COALESCE(o.delivery_address_id, o.invoice_address_id);
So, you want to join on delivery_address_id, but sometimes it's NULL, so you need invoice_address_id to be a fallback.
These situations are where the COALESCE function really shines. COALESCE(delivery_address_id, invoice_address_id) will resolve to the delivery address ID if it isn't NULL, otherwise it will resolve to the invoice address ID instead.
Thus we can achieve the join you want:
SELECT
orders.some_field,
addresses.address
FROM
orders
LEFT JOIN
addresses
ON
COALESCE(orders.delivery_address_id, orders.invoice_address_id) = addresses.id
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm brand new at SQL. I'm trying to display all the bikes that are colored black. I keep getting errors such as not unique tables/alias: Product. What am I missing?
SELECT Vendor.VendorID, Product.ProductID
FROM Product
INNER JOIN Product ON Vendor.VendorID = Product.ProductID
WHERE product.ProductColor = "Black";
SELECT Vendor.VendorID, Product.ProductID
FROM Product
INNER JOIN Product -- <-- this is the problem
ON Vendor.VendorID = Product.ProductID
WHERE product.ProductColor = "Black";
You meant to put Vendor there. Your server is complaining that you joined Product to itself but didn't tell it which of the two uses of "Product" to apply the where condition to.
Try the following. You do not have vendor table but you have mentioned that table column on join. Also, You should always use alias for the tables when you use join as it gives more readability.
SELECT v.VendorID, p.ProductID
FROM Product p
INNER JOIN Vendor v
ON v.VendorID = p.ProductID
WHERE p.ProductColor = 'Black';
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need some help with my exam, there are 2 questions but I don't know how to join them and which SQL statements I need.
Write a SQL statement which displays the employee id, last name,and salary of any employee whose salary is above the price of the most expensive product.
Write a SQL statement which displays the customer id of customers who purchased the product with product_id 1 but so far never purchased the product with product_id 3.
It would be nice if somebody explain me the solution.
With the understanding this is a practice exam, and that you don't have access to the tables/values....
You really should show what you've tried... but since you're new...
Question 1:
Return employeeID, last name and salary of all employees who have a salary greater than the price of the most expensive product (like how they avoided using the word max there...)
So this uses a subquery to get the max price from products and simply compares salary against it.
SELECT Employee_ID, Last_Name, Salary
FROM employees
WHERE salary > (Select max(price) from Products);
Question 2:
Generate two sets one for product_ID of 1 one for product_ID of 3 and make sure those with 1 are not in the group with 3.
and this uses a correlated subquery (notice how the PUR alias is in the subquery? that's why it's correlated)
So the first query gets all the customers buying product 1. The 2nd query finds all the customers buying product 3. and since we want only those customers who bought 1 not 3, we correlate the queries using not exists and we have our answer.
Exists - usually fastest as it can early exit the subquery once a single record for a customer is found.
SELECT Distinct PUR.Customer_ID
FROM purchases PUR
WHERE PRODUCT_ID = 1
and not exists (SELECT 1
FROM Purchases PUR2
WHERE Product_ID = 3
and PUR.Customer_ID = PUR2.Customer_I
There are 2-3 ways of doing this. I find the above the most efficient in most cases. However you could also do it by... a self join; or using an not IN statement
Self LEFT join (works great if you need data from multiple tables
We self join on purchases but filter each table instance to be for a specific product. we use left join as we want all records with product Id 1 and where a match is found in p2, we want to EXCLUDE those records so if the customer_ID is null that means they have no product 3 purchases
SELECT Distinct P1.customer_ID
FROM Purchases P1
LEFT JOIN Purchase P2
on P1.customer_Id = P2.customer_ID
and P2.Product_ID = 3
WHERE P1.Product_ID = 1
and P2.customer_ID is null
not in (usually slowest but works fine if subquery is for a VERY Small datasets)
SELECT Distinct PUR.Customer_ID
FROM purchases PUR
WHERE PRODUCT_ID = 1
and customer_ID not in (SELECT Customer_Id
FROM Purchases
WHERE Product_ID = 3)
Notice that neither of the 1st two answers uses a join; though a correlated subquery is close.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have been trying to figure this out for 4 days now. I am new to this and just can't this to work like it should. Any help would be appreciated.
*Not just looking for the answer
The president of the company wants a list of all orders ever taken. He wants to see the customer name, the last name of the employee who took the order, the shipper who shipped the order, the product that was ordered, the quantity that was ordered, and the date on which the order was placed. [Hint: You will need to join the following tables: Customers, Employees, Shippers, Orders, OrderDetails, Products, and to get all of the necessary information.]
Think about what you are trying to do here. You'll need to join together multiple tables to build the desired dataset. The type of JOINs you should be using will likely associate foreign keys from one table with the primary key of another. Please review JOINs if this is not clear.
Here is an example query that may get you on the right track. The column names used will likely vary, and I'll let you figure out how to get Quantity in the dataset (the quantity is probably in OrderDetails or perhaps you need to aggregate by product ID):
SELECT Customers.name, Employees.lastName, Shippers.name, Products.name,OrderDetails.orderDate
FROM Orders
JOIN OrderDetails ON OrderDetails.id = Orders.orderDetailId
JOIN Customers ON Customers.customerId = Orders.customerId
JOIN Employees ON Employees.employeeId = Orders.employeeId
JOIN Shippers ON Shippers.Id = OrderDetails.shipperId
JOIN Products ON OrderDetails.ProductId = Products.Id;
I should note that this is formatted for T-SQL and depending on your DBMS syntax may vary.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having a hard time joining three separate tables in Oracle. I'm never joined three tables before so I'm not well versed. My theory is below:
SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
ON order_num, stock_num
INNER JOIN stock
ON stock_num, description
Essentially, I'm trying to start with the ORDERS table and pull the customer number (customer_num) specifically customer number 104 -108 and the order_num from the orders table. Then attach the orders table to the Items table and attach the order_num and stock_num, and lastly attach the stock table and pull out the stock_num and description.
Your syntax is all over the place, and wouldn't work for a two-table join, or even querying a single table. Not sure where you got WHEN from, or the order of your clauses. Please review the SQL reference to see how to join and how to filter. Anyway...
You seem to want something like this:
SELECT o.customer_num, o.order_num, i.stock_num, s.description
FROM orders o
INNER JOIN items i ON i.order_num = o.order_num
INNER JOIN stock s ON s.stock_num = i.stock_num
WHERE o.customer_num BETWEEN 104 AND 198;
The WHERE clause is being applied to the orders table to restrict which customers' orders you get. I've assumed from your description that the orders and items tables have a common order_num column you can join on; and that the items and stock tables have a common stock_num column you can join on.
As OldProgrammer said, it would be helpful to include your table schemas in the question so assumptions don't need to be made, and showing sample data and expected output for that data would clarify what you're trying to do.
It should look something like this (based on the limited information about the schema)...
SELECT
O.CUSTOMER_NUM,
O.ORDER_NUM,
S.STOCK_NUM,
S.DESCRIPTION
FROM
ORDERS O,
ITEMS I,
STOCK S
WHERE
I.CUSTOMER_NUM >= 104
AND I.CUSTOMER_NUM <= 198
AND O.ORDER_NUM = I.ORDER_NUM
AND I.STOCK_NUM = S.STOCK_NUM
The syntax for a JOIN is:
TableExpression [ INNER ] JOIN TableExpression
{
ON booleanExpression | USING clause
}
The booleanExpression is what the tables will be linked by. So, in your example, something like this:
SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
ON orders.<some_column> = items.<some_column>
INNER JOIN stock
ON items.<some_column> = stock.<some_column>