SELECT S.SUPPLIER_NAME,P.PRODUCT_NAME,O.ORDER_ID,O.QUANTITY
FROM SUPPLIERS AS S
INNER JOIN PRODUCT AS P
ON S.PRODUCT_ID = P.PRODUCT_ID
INNER JOIN ORDERS AS O
ON P.PRODUCT_ID = O.ORDER_ID;
WHEN i am joining three table
ORA-00933: SQL command not properly ended.
Oracle does not use as for table aliases. Try this version:
SELECT S.SUPPLIER_NAME, P.PRODUCT_NAME, O.ORDER_ID, O.QUANTITY
FROM SUPPLIERS S JOIN
PRODUCT P
ON S.PRODUCT_ID = P.PRODUCT_ID JOIN
ORDERS O
ON P.PRODUCT_ID = O.ORDER_ID;
The syntax error comes from the S after the AS. The AS is considered the table alias, and a join condition or SQL clause (or horrors a comma) is expected next.
Oracle ALIASES can be used to create a temporary name for columns or tables.To make the heading of the output more meaningful and to improve readability of a query. Refer link
For example if you want to change the column 'SUPPLIER_NAME' As SUPPLIERNAME. This query will return the ResultSet column as 'SUPPLIERNAME'.
SUPPLIERS S --> S is temporary name for the table 'SUPPLIERS'. Refer below modification.
SELECT S.SUPPLIER_NAME,P.PRODUCT_NAME,O.ORDER_ID,O.QUANTITY
FROM SUPPLIERS S
INNER JOIN PRODUCT P
ON S.PRODUCT_ID = P.PRODUCT_ID
INNER JOIN ORDERS O
ON P.PRODUCT_ID = O.ORDER_ID;
Related
SELECT
(invoice.invoice_id) AS invoice_id,
COUNT (line_item.code) AS code,
SUM (price)
FROM
invoice
INNER JOIN
(line_item
INNER JOIN
(stock_item) ON line_item.code = stock_item.code) ON invoice.invoice_id = line_item.invoice_id;
I think the syntax you want is:
SELECT i.invoice_id AS invoice_id, COUNT(li.code) AS code, SUM(price)
FROM (line_item as li INNER JOIN
stock_item as si
ON li.code = si.code
) INNER JOOIN
invoice as i
ON i.invoice_id = li.invoice_id
GROUP BY invoice_id;
Note: I added table aliases so the query is easier to write and to read.
Hello I am trying to add a Translated_Name column from the Product_descriptions table to my current query that is already joining two tables however the translated_name column is a type NVARCHAR2. Should I be using Inner Join for it or am I completely wrong?
select order_mode,customer_id,product_id from ORDERS
inner join ORDER_items on order_items.ORDER_ID=Orders.ORDER_ID
where exists(select customer_id from customers where orders.customer_id=customers.customer_id)
inner join product_descriptions on product_descriptions.translated_name = Orders.Customer_id
The where clause goes after the joins:
select
order_mode,
customer_id,
product_id
from orders o
inner join order_items oi
on oi.order_id = o.order_id
inner join product_descriptions pd
on pd.translated_name = o.customer_id
where exists(
select 1
from customers c
where o.customer_id = c.customer_id
)
Notes:
table aliases make the query easier to read and write
you should qualify the columns the the from clause with the alias of the table they belong to
I am quite suspicious about the join condition on product_descriptions, which involves customer_id; you might need to review that (without knowing your table structures, it is not possible to tell what the correct condition is)
This query works:
SELECT product_name, unit_price, order_due_date
FROM orders
FULL OUTER JOIN products ON orders.product_id = products.product_id
WHERE product_name = 'bun';
whereas this one throws an error:
ORA-00936: "missing expression"
SELECT product_name, unit_price, order_due_date
FROM orders
FULL OUTER JOIN products ON orders.product_id = products.product_id
WHERE [category] = 'soft drink';
I can't figure what's wrong with the second
When you have multiple tables in a query, you should always qualify column names. In your first query, the WHERE clause is undoing the FULL JOIN. In fact, I am guessing that an INNER JOIN is most appropriate. How would an order have a product not in the products table?
SELECT p.product_name, ?.unit_price, o.order_due_date
FROM orders o INNER JOIN
products p
ON o.product_id = p.product_id
WHERE p.product_name = 'bun';
As for your second query, [ and ] are not delimiters in Oracle. Either leave them out:
WHERE category = 'soft drink'
Or, if for some obscure reason the column name actually has those characters, then use double quotes:
WHERE "[category]" = 'soft drink';
I'm trying to get only customers that ordered both a "Gas Range" and a "Washer". I'm getting Customers who ordered a "Gas Range" and not a "Washer" and customers with both. I need the customer that meets both conditions. I'm close but a little stuck. Below is the query that I have so far. Please let me know if you need more information.
My Tables - CUSTOMER(CUST_NUM, CUST_NAME), ORDER_LINE(ORDER_NUM, PART_NUM), ORDERS(ORDER_NUM, CUST_NUM), PART(PART_NUM, PART_DESCRIPTION)
SELECT C.CUST_NAME AS [Customer(s) that ordered a Gas Range and Washer]
FROM CUSTOMER C
INNER JOIN ORDERS O
ON C.CUST_NUM = O.CUST_NUM
INNER JOIN ORDER_LINE OL
ON O.ORDER_NUM = OL.ORDER_NUM
INNER JOIN PART P
ON OL.PART_NUM = P.PART_NUM
WHERE P.PART_DESCRIPTION IN ('GasRange','Washer')
GROUP BY C.CUST_NAME
try the following
SELECT C.CUST_NAME AS [Customer(s) that ordered a Gas Range and Washer]
FROM CUSTOMER C
INNER JOIN ORDERS O
ON C.CUST_NUM = O.CUST_NUM
INNER JOIN ORDER_LINE OL
ON O.ORDER_NUM = OL.ORDER_NUM
INNER JOIN PART P
ON OL.PART_NUM = P.PART_NUM
INNER JOIN ORDERS O2
ON C.CUST_NUM = O2.CUST_NUM
INNER JOIN ORDER_LINE OL2
ON O2.ORDER_NUM = OL2.ORDER_NUM
INNER JOIN PART P2
ON OL2.PART_NUM = P2.PART_NUM
WHERE P.PART_DESCRIPTION IN ('GasRange') and P2.PART_DESCRIPTION IN ('Washer')
GROUP BY C.CUST_NAME
EDIT: Had a further look and I'm afraid that this can't be simplified in any other way than using WITH and complicated aggregate functions, which I would say would be more complicated than this - I think the other solution suggested using WITH won't work - it joins incorrectly. You definitely can't remove order line, and you have to use the order twice as well - if it was used once, it will cover only when the customer ordered it within one order, which is not what you wanted ;)
Try this...
So basically you need to join your Parts table again to ensure the same customer ordered a "Gas Range" and a "Washer". An IN, like in your current query functions as an OR therefore you are not getting the expected result.
WITH CTE AS (
SELECT DISTINCT O.CUST_NUM FROM ORDERS O
INNER JOIN ORDER_LINE OL
ON O.ORDER_NUM = OL.ORDER_NUM
INNER JOIN PART P
ON OL.PART_NUM = P.PART_NUM
INNER JOIN PART P2
ON OL.PART_NUM = P2.PART_NUM
WHERE P.PART_DESCRIPTION IN ('GasRange')
AND P2.PART_DESCRIPTION IN ('Washer')
)
SELECT C.CUST_NAME AS [Customer(s) that ordered a Gas Range and Washer]
FROM CUSTOMER C
INNER JOIN CTE O
ON C.CUST_NUM = O.CUST_NUM
The following statement works properly but shows each record 4 times. Repeated; I know the relationship is wrong but no idea how to fix it? Apologies if this is simple and i've missed it.
SELECT Customers.First_Name, Customers.Last_Name, Plants.Common_Name, Plants.Flower_Colour, Plants.Flowering_Season, Staff.First_Name, Staff.Last_Name
FROM Customers, Plants, Orders, Staff
INNER JOIN Orders AS t2 ON t2.Order_ID = Staff.Order_ID
WHERE Orders.Order_Date
BETWEEN '2011/01/01'
AND '2013/03/01'
You are generating a Cartesian product between the tables since you have not provided join syntax between any of the tables:
SELECT c.First_Name, c.Last_Name,
p.Common_Name, p.Flower_Colour, p.Flowering_Season,
s.First_Name, s.Last_Name
FROM Customers c
INNER JOIN Orders o
on c.customerId = o.customer_id
INNER JOIN Plants p
on o.plant_id = p.plant_id
INNER JOIN Staff s
ON o.Order_ID = s.Order_ID
WHERE o.Order_Date BETWEEN '2011/01/01' AND '2013/03/01'
Note: I am guessing on column names for the joins
Here is a great visual explanation of joins that can help in learning the correct syntax
In the FROM... clause you are doing a cross join - combining every customer with every plant with every order with every staff.
You should only mention one table in the FROM clause and then connect the other ones with INNER JOINS to only get related records.
I don't know exactly how your database looks like, but something like this:
SELECT Customers.First_Name, Customers.Last_Name, Plants.Common_Name,
Plants.Flower_Colour, Plants.Flowering_Season, Staff.First_Name, Staff.Last_Name
FROM Customers
INNER JOIN Orders ON Orders.Customer_ID = Customers.Customer_ID
INNER JOIN Staff ON Staff.Staff_ID = Orders.Staff_ID
INNER JOIN Plants ON Plants.Plants_ID = Orders.Plants_ID
WHERE Orders.Order_Date
BETWEEN '2011/01/01'
AND '2013/03/01'
This is because you are selecting from four tables without any joins between them, and also because you are joining Orders twice. As the result, a Cartesian product is made.
Here is how you should fix it: re-write the theta join using the ANSI syntax, and provide proper join conditions:
SELECT Customers.First_Name, Customers.Last_Name, Plants.Common_Name, Plants.Flower_Colour, Plants.Flowering_Season, Staff.First_Name, Staff.Last_Name
FROM Customers
JOIN Plants ON ...
JOIN Orders ON ...
JOIN Staff ON ...
INNER JOIN Orders AS t2 ON t2.Order_ID = Staff.Order_ID
WHERE Orders.Order_Date BETWEEN '2011/01/01' AND '2013/03/01'
Replace ... with proper join conditions; this should make the results look as expected.