I am taking a SQL database class using Oracle. There’s an assignment:
“Find the book title, author last name, and units on hand for each book in branch number 4”
I’ve tried different ways of nesting statement but none have worked with these tables.
- BOOK
- AUTHOR
- INVENTORY
- BRANCH
That was very helpful! (do actually want to learn this rather than just complete my homework) So far I got. besides if I wanted just the answers I would have said so,…
select book.book_code, author.author_last, inventory.on_hand
from book inner join wrote on book.book_code = wrote.book_code,
author inner join wrote on author.author_num = wrote.author_num,
inventory inner join branch on inventory.branch_num = branch.branch_num
where branch.branch_num = 4;
But now I have a bunch of repeating products
You don’t need to nest. You need to join.
You’ll probably need something like this, although I would need the exact table structure to be sure. But you’ll get the general idea.
Some people would argue that BRANCH should be the first table, because you’re filtering on that. You can do that if you like and if you think it makes more sense. In some circumstances, it may result in better performance, although generally the query optimizer takes care of it.