Need help trying to figure this out [closed] - sql

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.

Related

How do I determine which table should be to the left of my join?

I am practicing SQL joins on learnsql.com and when joining three tables, I'm having trouble determining which table should be to the left of the join clause and which should be to the right.
Show the name of each product and its calorific value for all products that in the ‘dairy’ department.
Since the question begins by asking for the name of each product, I made the assumption that my answer should be ... FROM product p .... and then I would left join the nutrition_data and department data on it.
List all products that have fewer than 150 calories. For each product show its name (rename the column to product) and the department in which can be found (name the column department).
I interpreted the second question the same way, that it should be a list of all the products (... FROM product ...) and then the department and nutrition_data would be joined to that.
Below is a picture of the tables and the answer to the first question, the wrong answer to the second question and the right answer to the second question.
https://imgur.com/a/qLqpjGo
Because the question asked for a list of all the products, I thought the product table should be the first table after the FROM clause and then all the other tables would be joined to it. This logic worked for the first question, and since the second question is similar in nature, I assumed it was the same logic. However for the second, the solution is ... FROM department ... and then the product and nutrition_data is joined to the department table.
How do I determine which table should be to the left of my join?
"all products" is what calls an outer join: all products even when they don't have calorific information
"all products that have fewer than 150 calories" cannot show the products without calorific information. Then I see no outer join here. Except if no calorific information means no calories

ambiguous column name error [closed]

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).

With what groupings am I supposed to think about joins? [closed]

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 7 years ago.
Improve this question
I'm looking at the example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
from W3Schools and am wondering how I'm supposed to group the clauses in my mind. From what I understand, every SQL query returns a table, and clauses within the query may themselves return a table. So I think of the whole
Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID
as being a table and I'm returning a sub-table of it by applying SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate to it. Is that the right way to think about things?
You can think of every query as if it composes a new temporary table with the result you want. This one takes rows from Orders and from Customers, matches them according to the CustomerID field in both tables (the on clause of the join), and then returns just several fields from Orders (OrderID and OrderDate) and one field from Customers (CustomerName).
Basically, you are returning a new table containing all of the columns in your SELECT statement. If you simplify the query to:
SELECT OrderID, OrderDate
FROM Orders
You'd get a printout of the entire Orders table, but with only the two columns you specified.
With your INNER JOIN, you are filtering out the results to only include the Orders that have a CustomerID that matches a similar row in the Customers table with that same CustomerID. And with your SELECT statement, you are adding a column that shows the customer's name that is matched up with those orders.
So the ON part of the INNER JOIN is just matching customers to their respective orders. The SELECT part is where you are creating the new output table with the three columns you are interested in.
Does that help?

How do you join three tables in Oracle? [closed]

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>

multiple calculations across tables [closed]

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 8 years ago.
Improve this question
I am trying to get the total cost of all the invoices for a customer. Ideally the end format will be two columns [customer name] and [total of invoices]. I have broken it down into the parts so far so I can check and better understand the process of joining the tables and have done a calculation to get the total of items on each invoice but now I am stuck.
As you can see from my screenshot ( Had to link to my google docs as I couldn't post the image up here - sorry) I am getting the company name listed multiple times. Once for each item and also for each invoice number and then item. How can I change my query to show the customer name only once with the corresponding totals of all the invoices combined?
I have lines 3 and 4 as comments of what I think is next so I can work this in steps before fine tuning the query to my desired output.
Thanks
Select Customer.CustName, Sum(InvoiceItem.Quantity*Item.ItemPrice) As TotalValue
From Customer
Inner Join Invoice On Customer.CustABN = Invoice.CustABN
Inner Join InvoiceItem On Invoice.InvoiceNo = InvoiceItem.InvoiceNo
Inner Join Item On InvoiceItem.ItemNo = Item.ItemNo
Group By Customer.CustName
Something like this should work using SUM and GROUP BY:
SELECT CustomerName, SUM(itemPrice * qty) InvoiceTotal
FROM YourTables With Your Joins
GROUP BY CustomerName
If you posted your entire query above, I could copy and paste into the example. But this should get you going in the right direction.
grouping could help, also you need to check if your dbms allows grouping without using agregate functions (some DBMS do not allw it, and return misleading results).
multiple companies is because of the relation company-invoice-product i guess.