Using the BOOK_ORDER, ORDER_ITEMS, and BOOKS tables, create a query using an OUTER JOIN operation that will list the book title, order date, and order number for all books in the BOOKS table. Order your output in descending order by ORDER_ITEMS.BOOKID. There are three books that have never been ordered which should show up at the top of your listing.
I wrote this query:-
SELECT B.TITLE, BO.ORDERDATE, ORD.ORDERID FROM BOOKS B, BOOK_ORDER BO, ORDERITEMS ORD
WHERE B.BOOKID = ORD.BOOKID
AND BO.ORDERID = ORD.ORDERID(+)
ORDER BY ORD.ORDERID DESC
I am getting the results but confused about this following part of the question:-
“There are three books that have never been ordered which should show up at the top of your listing.”
I am guessing this mean I need to display those Books that have Orderdate and OrderId as NULL or blank. But how do I get those rows on top of the result set? What should I change in the query that I have written?
This is the BOOKS table:-
Name Null? Type
----------------------------------------- -------- ---------------
BOOKID NOT NULL NUMBER(15)
ISBN VARCHAR2(10)
TITLE VARCHAR2(30)
PUBDATE DATE
PUBID NUMBER(2)
COST NUMBER(5,2)
RETAIL NUMBER(5,2)
CATEGORY VARCHAR2(12)
This is ORDER_ITEMS table:-
Name Null? Type
----------------------------------------- -------- ----------------
ORDERID NOT NULL NUMBER(4)
ITEMNUM NOT NULL NUMBER(2)
BOOKID NOT NULL NUMBER(15)
QUANTITY NUMBER(3)
and here is BOOK_ORDER table structure:-
Name Null? Type
----------------------------------------- -------- --------------
ORDERID NOT NULL NUMBER(4)
CUSTOMERID NUMBER(4)
ORDERDATE DATE
SHIPDATE DATE
SHIPSTREET VARCHAR2(20)
SHIPCITY VARCHAR2(20)
SHIPSTATE VARCHAR2(2)
SHIPZIP VARCHAR2(5)
try this:
By the way, IMHO, it’s better not to use the
(+)operator but the ANSIJOINkeywords:here is a fiddle