I'm also well aware of the issues in it. Exam question I ran across.
SELECT customer#, SUM(quantity*retail) "Total Sales"
FROM orders JOIN orderitems ON orders.order# = orderitems.order#
JOIN books ON orderitems ON orderitems.ISBN = books.ISBN
HAVING orderdate = '02-APR-09'
GROUP BY customer#;
SELECT customer#, SUM(quantity*retail) "Total Sales"FROM orders JOIN orderitems ON orders.order# = orderitems.order#JOIN books ON orderitems ON orderitems.ISBN = books.ISBNHAVING orderdate = '02-APR-09'GROUP BY customer#;
Too much to mention, but I can try:
Having clause must always be after the group by clause, it looks as if it should be a WHERE
JOIN books ON orderitems ON orderitems.ISBN -> ON something ON is not valid syntax.
'02-APR-09' is not a deterministic date format, it's not an end of the world error, but you don't know what date SQL will use without knowing something about the server setup.
The # in customer# and order# is legal, but rarely found in production databases. So much so that I had to check, and I've been working with SQL for 20 years, I have never seen a field name with a #, I've seen field names with spaces though.
orderdate is not included in the GROUP BY, so you can't use it in the HAVING clause.
The join on books is present but never used, so it is not serving any useful function.
It's probably supposed to be something like this:
SELECT customer#, SUM(quantity*retail) "Total Sales"
FROM orders
JOIN orderitems ON orders.order# = orderitems.order#
JOIN books ON orderitems.ISBN = books.ISBN
WHERE orderdate = '02-APR-09'
GROUP BY customer#;
13
u/[deleted] Aug 01 '22
I'm also well aware of the issues in it. Exam question I ran across.