Derived tables, ANY & ALL
◈ 2 cardsQuery a subquery as if it were a table; compare against a set.
A subquery as a table
A subquery in the FROM clause is a derived table — you query its result like any other table (it needs an alias):
SELECT genre, avg_price
FROM (
SELECT genre, avg(price) AS avg_price FROM books GROUP BY genre
) g
WHERE avg_price > 15;
ANY and ALL compare a value against every value a subquery returns:
- price > ALL (...) — greater than every one
- price > ANY (...) — greater than at least one (= ANY is the same as IN)