Learn why wrapping a window function in a CTE lets you filter on its result in the outer query.
You write: WITH ranked AS (SELECT emp_id, dept, salary, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn FROM employees) SELECT * FROM ranked WHERE rn <= 3; Why does the WHERE rn <= 3 filter work here, even though you cannot put a window function directly in a WHERE clause?