8

I'm studying multiple table joins this week and have some odd results being returned. Here is the scenario...

Using the correct tables, create a query using the traditional join operation that will list the customer first and last name, book title, and order date (formatted as MM/DD/YYYY with an alias of “Order Date”) for all the customers who have purchased books published by 'PRINTING IS US'.

With the database that I'm querying against, the correct tables for this query are BOOK_CUSTOMER, BOOKS, BOOK_ORDER, and PUBLISHER. The statement that I have written returns the information that I need, but it is returning almost 5900 records. I don't see how this can be right. The publisher, Printing is Us, only has 14 books listed in the database and there are only 20 customer records, so even if every customer purchased every Printing is Us book, that would only return 280 records total. Yet I can't figure out what I have wrong. My statement is listed below.

SELECT bc.firstname, bc.lastname, b.title, TO_CHAR(bo.orderdate, 'MM/DD/YYYY') "Order Date", p.publishername
FROM book_customer bc, books b, book_order bo, publisher p
WHERE(publishername = 'PRINTING IS US');

Anyone have any thoughts on what I'm missing here??

Thanks.

2
  • 1
    You are missing a JOINclause. Or rather three of them. Commented Jun 7, 2014 at 18:41
  • Close - he's missing the comparisons. Effectively he has the JOIN clauses, but not the ON clause. Granted, it's best to avoid the implicit-join syntax (comma-separated FROM clause), but at least the class seems to be referring to it as a "traditional" join. Commented Jun 8, 2014 at 3:53

3 Answers 3

18

I recommend that you get in the habit, right now, of using ANSI-style joins, meaning you should use the INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, and CROSS JOIN elements in your SQL statements rather than using the "old-style" joins where all the tables are named together in the FROM clause and all the join conditions are put in the the WHERE clause. ANSI-style joins are easier to understand and less likely to be miswritten and/or misinterpreted than "old-style" joins.

I'd rewrite your query as:

SELECT bc.firstname,
       bc.lastname,
       b.title,
       TO_CHAR(bo.orderdate, 'MM/DD/YYYY') "Order Date",
       p.publishername
FROM BOOK_CUSTOMER bc
INNER JOIN books b
  ON b.BOOK_ID = bc.BOOK_ID
INNER JOIN  book_order bo
  ON bo.BOOK_ID = b.BOOK_ID
INNER JOIN publisher p
  ON p.PUBLISHER_ID = b.PUBLISHER_ID
WHERE p.publishername = 'PRINTING IS US';

Share and enjoy.

2
  • Given that his course material referred to it as "traditional" style joins, I have a feeling he's required to use the implicit syntax. Granted, I agree it should be avoided (and the OP now has a prime example of why), but new students have to learn what the syntax means somehow. Commented Jun 8, 2014 at 3:55
  • You guys explained that better than the textbooks and lectures have done all term. Thank you. Yes, Clockwork-Muse, you're right, we're under instruction to use the exact method called for in the query question, but I do understand the need to get into the habit of using ANSI syntax. Thanks again guys. Commented Jun 8, 2014 at 5:06
4

You are doing a cartesian join. This means that if you wouldn't have even have the single where clause, the number of results you get would be book_customer size times books size times book_order size times publisher size.

In order words, the result set gets blown up because you didn't add meaningful join clauses. Your correct query should look something like this:

SELECT bc.firstname, bc.lastname, b.title, TO_CHAR(bo.orderdate, 'MM/DD/YYYY') "Order Date", p.publishername
FROM book_customer bc, books b, book_order bo, publisher p
WHERE bc.book_id = b.book_id
AND bo.book_id = b.book_id
(etc.)
AND publishername = 'PRINTING IS US';

Note: usually it is adviced to not use the implicit joins like in this query, but use the INNER JOIN syntax. I am assuming however, that this syntax is used in your study material so I've left it in.

2

While former answer is absolutely correct, I prefer using the JOIN ON syntax so I am sure how I join and on what fields. It would look something like this:

SELECT bc.firstname, bc.lastname, b.title, TO_CHAR(bo.orderdate, 'MM/DD/YYYY') "Order         Date", p.publishername
FROM books b
JOIN book_customer bc ON bc.costumer_id = b.book_id
LEFT JOIN book_order bo ON bo.book_id = b.book_id
(etc.)
WHERE b.publishername = 'PRINTING IS US';

This syntax separates completely the WHERE clause from the JOIN clause, making the statement more readable and easier for you to debug.

4
  • Um, why are you using both the implicit syntax and explicitly listing joins? I'm assuming it's a typo, due to the fact that you reuse a table alias (so would get an error). I don't think that the query as stated needs a LEFT JOIN, just regular (INNER) JOINs. Commented Jun 8, 2014 at 3:50
  • @Clockwork-Muse I don't need the LEFT JOIN, I just wanted to show it. As for the table aliases - I'm correcting it. No need to do it twice.
    – Amir Sagiv
    Commented Jun 8, 2014 at 4:11
  • @Clockwork-Muse the idea is that the FROM clause consists of one table only, and the rest come in the JOIN cluase.
    – Amir Sagiv
    Commented Jun 8, 2014 at 4:12
  • This is very useful!
    – jim_z
    Commented Aug 27, 2021 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.