Lesson 1 of 4

Progress: 0/3 exercises solved (0%)

Solve all exercises below to unlock the next lesson.

ROW_NUMBER and RANK

Lesson Description

Number rows within a partition.

Easy Project

Mini project: Build a compensation ranking dashboard query with department-level rank and delta from the top earner.

ROW_NUMBER and RANK — ranking within groups

Window functions compute a value for each row based on a set of related rows (a "window") without collapsing the result. The OVER() clause defines the window.

OVER() — the window clause

function() OVER (
  PARTITION BY col   -- divide rows into groups
  ORDER BY    col    -- sort within each group
)

ROW_NUMBER() — unique sequential integers

Assigns 1, 2, 3, … with no ties. Ties get different numbers (arbitrary order):

SELECT
  first_name, department_id, salary,
  ROW_NUMBER() OVER (
    PARTITION BY department_id
    ORDER BY salary DESC
  ) AS dept_rank
FROM employees;

RANK() — ties get the same number, next rank skips

Employees tied at salary share a rank; the next rank jumps:

salary  RANK
12000   1
12000   1
9000    3    skipped 2

DENSE_RANK() — ties share rank, no gaps

salary  DENSE_RANK
12000   1
12000   1
9000    2    no gap

Choosing between them

FunctionTiesGaps after ties
ROW_NUMBERUnique numbers
RANKSame numberYes
DENSE_RANKSame numberNo

Filtering to top-N per group

Window functions cannot appear in WHERE. Wrap in a CTE or subquery:

WITH ranked AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rn
  FROM employees
)
SELECT * FROM ranked WHERE rn = 1;

Tips

  • PARTITION BY is optional. Omitting it treats the whole result set as one partition.
  • Multiple window functions in the same query share OVER() — each is independent.

Learning context

This lesson is part of Window Functions. Focus on the core idea in ROW_NUMBER and RANK, then validate with deliberate practice.

What to master

  • Understand clause order and intent
  • Write deterministic result queries
  • Use clear aliases and readable structure

Common mistakes

  • Skipping ORDER BY in final outputs
  • Using joins before validating base rows
  • Not checking row counts after changes

High-level strategy

  • Start simple, then layer complexity
  • Run often and compare expected shape
  • Refactor with CTEs when logic grows

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

WHERE

Why: Limits rows to only the business-relevant subset.

Memory cue: Filter early to reduce noise.

ORDER BY

Why: Makes output deterministic and reviewable.

Memory cue: No ORDER BY means no guaranteed row order.

AS alias

Why: Makes output columns readable for teams and reports.

Memory cue: If the name is clear, the query is easier to trust.

Concept check

Quick check: ROW_NUMBER and RANK

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Rank employees within each department by salary

Enterprise Workforce Analyticshard

Return first_name, last_name, department_id, salary, and a column dept_rank that ranks employees within each department by salary descending (use ROW_NUMBER).

Run a query to see results here.
Exercise 2

RANK with salary ties within department

Enterprise Workforce Analyticseasy

Return first_name, last_name, department_id, salary, and salary_rank using RANK() (not ROW_NUMBER) within each department ordered by salary descending.

Run a query to see results here.
Exercise 3

Top earner in each department

Enterprise Workforce Analyticseasy

Using a CTE with ROW_NUMBER(), return only the highest-paid employee per department. Show first_name, last_name, department_id, and salary, ordered by department_id.

Run a query to see results here.

Lesson 1 of 4

0/3 solved (0%)

Finish this lesson to unlock next.