Sorts Within Sorts

In the first ORDER BY query, you sorted the result rows by descending price (more about ascending and descending sorts in the next section). What if you also want to make sure books within each price category by the same publisher are listed together? Adding pub_id to the ORDER BY list is the way to do it. Separate price and pub_id with a comma.

SQL
select title_id, pub_id, price * ytd_sales as Income, price
from titles
where type in ('business', 'psychology')
order by price desc, pub_id title_id pub_id Income price ======== ====== ============== ========== PS1372 0736 15596.25 41.59 PS3333 0736 122119.28 29.99 BU1032 1389 122809.05 29.99 BU7832 1389 122809.05 29.99 PS2091 0736 44887.75 21.95 BU1111 1389 85078.20 21.95 PS7777 ...

Get Practical SQL Handbook, The: Using SQL Variants, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.