GROUP BY and HAVING Must Return a Single Value

Comparison operator subqueries cannot include GROUP BY and HAVING clauses unless you know that they return a single value. For example, this query finds the books priced the same as the lowest-priced book in any category in which the minimum price is one-200th of the minimum advance. The subquery finds one qualifying row so there is no problem.

SQL
select title, type, price
from titles
where price =
   (select min(price)
   from titles
   group by type
   having min (price) < min(advance) /200  )
title                                         type            price
============================================= =============== =======
You Can Combat Computer Stress!               business        12.99
The Gourmet Microwave                         mod_cook        12.99
[2 rows]

If you modify the HAVING clause to ...

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.