WHERE, GROUP BY, HAVING, ORDER BY

The following statement adds an ORDER BY and gives you all clauses in one SELECT statement. It produces the same groups and summary values as the previous example but orders the results by pub_id:

SQL
select pub_id, sum(advance), avg(price)
from titles
where  pub_id > '0800'
group by pub_id
having sum(advance) > 15000
 and avg(price) > 20
order by pub_id
pub_id sum(titles.advance) avg(titles.price)
====== =================== =================
0877              34000.00             27.17
1389              30000.00             32.98
[2 rows]

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.