SQL QuestSQL Interview Questions › Window Functions

Window Functions: ROW_NUMBER

MediumFreeQuerying BasicsWindow Functions

Window functions calculate values across rows without collapsing them (unlike GROUP BY). Add a row_num to each employee within their department, ordered by salary descending, then name for ties (highest paid = 1). Show name, department, salary, and row_num. Sort by department, row_num. This is the gateway to all window functions.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

employees

emp_idnamedepartmentpositionsalaryhire_datemanager_idperformance_rating
1Alice JohnsonEngineeringSenior Developer950002019-03-1554.5
2Bob SmithEngineeringDeveloper750002020-06-0113.8
3Carol WilliamsMarketingMarketing Manager850002018-09-20NULL4.2

Expected output: Alice row_num=1, Bob row_num=2

Hint

ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num. PARTITION BY creates separate numbering per department. OVER() defines the window.

Concepts

SELECT Window Functions ROW_NUMBER

Practise the topic: SQL practice questions · Window function practice · Ranking function practice

In these company practice sets

Anthropic · OpenAI · Snowflake · Uber · Microsoft

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Month-over-Month Customer GrowthMedium · FreeRANK vs DENSE_RANK Side-by-SideMedium · FreeTop 3 Salary Tiers (DENSE_RANK)Medium · FreeMost Recent Order Per Customer (ROW_NUMBER)Medium · FreeSecond-Highest Earner Per Department (ROW_NUMBER)Medium · FreeSalary vs Department Average (PARTITION BY)Medium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test