EasySubqueries & EXISTS

Find products priced above average

The query

SQL
SELECT name, price, cat_id
FROM products
WHERE price > (SELECT AVG(price) FROM products WHERE is_active = TRUE)
ORDER BY price DESC;
Tested against PostgreSQL 16

Note

Scalar subquery for threshold computation. Cleaner than pre-computing average in application code.

Tables referenced