Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > SQL query for co-authored books
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

SQL query for co-authored books

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 19 June 2008
My book table has 4 instances of co-authored books, but I cannot retrieve them. So in other words, I'm trying to list the book (id, title, and cost) and author (id, name) and publisher (id, name) info for all co-authored books in the collection. This is what I have so far to join the tables together:

SELECT book.id, book.title, book.cost,
       author.id, author.name,
       publisher.id, publisher.name
FROM book, author, publisher, writtenby
WHERE book.id = writtenby.bookid
AND writtenby.authorid = author.id
AND book.publid = publisher.id;

I'm not sure where to go from here to get the four rows.


>
Whenever a query involves a number of tables, I find it helpful always to start writing the join with the most restrictive table first.

For example, if you are writing a query to retrieve all the books from a specific publisher, the restriction in this query would be the specific publisher. Think of it as the difference between:

  • Retrieve all books, then retrieve the publisher for each book, and then throw away both the book and the publisher if the publisher isn't the right one.

versus

  • Retrieve the specific publisher, then retrieve only the books for that publisher.

Does that make sense? See the difference?

Okay, so my rules for writing the FROM clause are:

  1. Start with the most restrictive table first.

  2. If the first table has any many-to-one relationships, join those tables next. Specifically, for each row of the first table, there is at most one row of the joined table(s).

  3. If any of the tables thus far joined has any one-to-many relationships, you can join only one of these. Otherwise, if you join more than one one-to-many table, you get cross join effects.

  4. Continue by joining many-to-one tables as necessary.

So, in your query, the table you want to start with is books. Why is this most restrictive? Because you want only co-authored books, and there are many fewer co-authored books than those written by a single author.

Next, join to publishers, because a given book has just one publisher. (If a book has two publishers, it isn't the same book. It might have the same name, but it'll have a different ISBN.)

Now you can join to writtenby, which is your books-to-authors relationship table. Specifically, for a given book, it is a one-to-many relationship.

Finally, you can join to authors, because from writtenby to authors, it's a many-to-one relationship.

This should all make a lot more sense if you were to abandon the old style FROM clause comma-delimited list of tables with WHERE conditions, and started using JOIN syntax:

SELECT book.id
     , book.title
     , book.cost
     , author.id
     , author.name
     , publisher.id
     , publisher.name
  FROM book
INNER
  JOIN publisher
    ON publisher.id = book.publid
INNER
  JOIN writtenby
    ON writtenby.bookid = book.id
INNER
  JOIN author
    ON author.id = writtenby.authorid

Doesn't that look better? The intent is certainly clearer, I think.

Now the only remaining task is to restrict the first table, the books table, to only co-authored books. That requires a subquery:

 WHERE book.id IN
       ( SELECT bookid
           FROM writtenby
         GROUP 
             BY bookid
         HAVING COUNT(*) > 1 )

The subquery is uncorrelated, which means it can be executed before the main query, to produce a list of book ids for only co-authored books. Thus the main query is most efficient because it retrieves the publisher and author data only for those books.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle development languages
How to check SQL query construction with the Mimer Validator
Understanding SQL string functions
The top advice from Oracle experts in 2008
What's the difference between an SQL inner join and equijoin?
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts