Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumSQLwindow functions ROW_NUMBER RANKSingle-choice MCQ

You want only the top-2 earners per department from a ROW_NUMBER query. Why must the filter go in an outer query rather than the WHERE clause of the same SELECT?

SELECT * FROM ( SELECT name, dept, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn FROM emp ) t WHERE rn <= 2;