SELECT product_name, price
FROM products
WHERE price >ALL(SELECTAVG(price)FROM products
GROUPBY category_id
);
Output
-- Products priced above every category's average
Note ANY means the condition must be true for at least one value from the subquery. ALL means it must be true for every value. If the subquery returns an empty set, ALL conditions are true and ANY conditions are false.
Frequently asked questions
How does SQL handle greater than?
SQL covers this with 2 copy-ready snippets on this page. The "Comparison Operators" snippet in SQL uses `WHERE column = | <> | < | > | <= | >= value`.
Which statement does the SQL example use?
The "Comparison Operators" snippet uses `WHERE column = | <> | < | > | <= | >= value`, from the Filtering section of the SQL cheat sheet.
What other SQL snippets are shown for "greater than"?
Besides "Comparison Operators", this page also shows "ANY and ALL".
Is there anything to watch out for?
Yes. For "Comparison Operators": Use <> for not-equal (ANSI standard). != works in MySQL and PostgreSQL but is not part of the standard.