TheAnalyst / SQLAll lessons ↗
Day 18 · Advanced SQL · 10–20 minutes

Window functions: ranking

Window functions calculate across related rows without collapsing them into groups.

01 · Learn

The idea

Window functions calculate across related rows without collapsing them into groups. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.

SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank FROM employees ORDER BY department, salary_rank;
Read it actively

Before running the example, predict the output columns and whether the query returns one row, many rows, or possibly no rows.

02 · Recall

Explain it without looking

In your own words, explain the main SQL feature in today’s lesson and why it answers the question.

03 · Solve

Run your own query

Rank employees by salary within each department, highest salary first.

Loading browser SQL engine…
04 · Repeat

Close the loop