WHERE price > (SELECT AVG(price) FROM products) compares each row with a single computed value.
IN (SELECT customer_id FROM orders) checks membership in a list that another query produces.
Worked example
Which products cost more than the average product?
SELECT name, price FROM products WHERE price > (SELECT AVG(price) FROM products) ORDER BY price DESC;
| name | price |
|---|---|
| 14-inch laptop | 1199 |
| Mini PC | 549 |
| Standing desk | 499 |
| Ergonomic chair | 389 |
| 27-inch monitor | 329 |
Common mistakes
- Using = with a subquery that returns several rows.
- Using NOT IN with a list that contains NULL, which returns nothing.