Top N per Group
SELECT n_name FROM nation WHERE rank() OVER (ORDER BY n_name) <= 3;
SELECT year, o_orderkey, o_totalprice, rank
FROM (
SELECT extract(year FROM o_orderdate) AS year, o_orderkey, o_totalprice,
rank() OVER (PARTITION BY extract(year FROM o_orderdate)
ORDER BY o_totalprice DESC)
FROM orders
) AS ranked
WHERE rank <= 3
ORDER BY year, rank;
WHERE runs before the windows:
rank in a subquery, then filter outside (21 rows)