Skip to content
Hands-on Mentor
ENTR
Back to blog

SQL window functions explained: ROW_NUMBER, RANK and running totals

SQL window functions calculate across a group of rows and write the result next to every row. Learn OVER, PARTITION BY, RANK and running totals with examples.

6 min read

SQL window functions calculate something across a group of related rows and put the result next to each row, without collapsing the rows. GROUP BY turns many rows into one summary row. A window function keeps every row and adds a new column. You can spot one by its OVER (...) clause.

All examples use the Northbridge University database from the SQL course, and two tables: enrollment (one row per student and course offering, with exam scores) and student.

What is the difference between GROUP BY and a window function?

With [GROUP BY](/blog/sql-group-by), you lose the individual students. Here is the average midterm score for two course offerings:

SELECT offering_id, ROUND(AVG(midterm), 1) AS avg_midterm
FROM enrollment
WHERE offering_id IN (3, 4)
GROUP BY offering_id
ORDER BY offering_id;
offering_idavg_midterm
378.0
486.8

What if you want each student's score next to the average of their own offering? Use AVG(...) OVER (...):

SELECT s.first_name || ' ' || s.last_name AS student,
       e.offering_id,
       e.midterm,
       ROUND(AVG(e.midterm) OVER (PARTITION BY e.offering_id), 1) AS offering_avg,
       ROUND(e.midterm - AVG(e.midterm) OVER (PARTITION BY e.offering_id), 1) AS diff
FROM enrollment e
JOIN student s ON s.id = e.student_id
WHERE e.offering_id IN (3, 4)
ORDER BY e.offering_id, e.midterm DESC;
studentoffering_idmidtermoffering_avgdiff
Kenji Park38878.010.0
Lucas Okafor37578.0-3.0
Ayesha Kowalski37178.0-7.0
Liam Johansson49586.88.2
Diego Haddad48786.80.2
Omar Nguyen48686.8-0.8
Diego Rossi48586.8-1.8
Ayesha Kowalski48186.8-5.8

All 8 rows are still there. PARTITION BY e.offering_id means "calculate the average separately for each offering". The partition is the group that the row belongs to.

How does ROW_NUMBER() OVER (PARTITION BY ...) work?

ROW_NUMBER() gives each row a number inside its partition. The ORDER BY inside OVER decides who gets 1, 2, 3 and so on.

SELECT s.first_name || ' ' || s.last_name AS student,
       e.offering_id,
       e.midterm,
       ROW_NUMBER() OVER (PARTITION BY e.offering_id ORDER BY e.midterm DESC) AS place
FROM enrollment e
JOIN student s ON s.id = e.student_id
WHERE e.offering_id IN (3, 4)
ORDER BY e.offering_id, place;
studentoffering_idmidtermplace
Kenji Park3881
Lucas Okafor3752
Ayesha Kowalski3713
Liam Johansson4951
Diego Haddad4872
Omar Nguyen4863
Diego Rossi4854
Ayesha Kowalski4815

The numbering starts again at 1 in each offering. This is the classic way to answer "who is first in each group?". You cannot filter on place in the same query, because WHERE runs before window functions. PostgreSQL tells you so:

Outputwindow functions are not allowed in WHERE

Calculate the number in a subquery (or a [CTE](/blog/sql-cte-with-clause)) and filter outside:

SELECT *
FROM (
  SELECT s.first_name || ' ' || s.last_name AS student,
         e.offering_id,
         e.midterm,
         ROW_NUMBER() OVER (PARTITION BY e.offering_id ORDER BY e.midterm DESC) AS place
  FROM enrollment e
  JOIN student s ON s.id = e.student_id
  WHERE e.offering_id IN (3, 4)
) ranked
WHERE place = 1
ORDER BY offering_id;
studentoffering_idmidtermplace
Kenji Park3881
Liam Johansson4951

What is the difference between RANK and DENSE_RANK?

You only see a difference when two rows have the same value. Offering 1 has two students with a midterm score of 70:

SELECT s.first_name || ' ' || s.last_name AS student,
       e.midterm,
       ROW_NUMBER() OVER (ORDER BY e.midterm DESC) AS row_number,
       RANK() OVER (ORDER BY e.midterm DESC) AS rank,
       DENSE_RANK() OVER (ORDER BY e.midterm DESC) AS dense_rank
FROM enrollment e
JOIN student s ON s.id = e.student_id
WHERE e.offering_id = 1
ORDER BY e.midterm DESC, student;
studentmidtermrow_numberrankdense_rank
Diego Rossi95111
Liam Johansson93222
Kenji Park89333
Diego Haddad78444
Ayesha Kowalski70555
Lucas Okafor70655
Omar Nguyen68776
Nikolai Haddad54887
  • ROW_NUMBER always gives different numbers, even for a tie. Which tied row gets 5 is arbitrary, so add a second column to the ORDER BY if you need a stable result.
  • RANK gives tied rows the same number, then skips: 5, 5, 7.
  • DENSE_RANK gives tied rows the same number and does not skip: 5, 5, 6.

Use RANK for a contest-style ranking, DENSE_RANK for "which distinct score level is this?", and ROW_NUMBER when every row needs its own number.

How do you calculate a running total in SQL?

Put an ORDER BY inside OVER and the calculation grows row by row. This query counts how many students had signed up for offering 1 by each row, in sign-up order:

SELECT s.first_name || ' ' || s.last_name AS student,
       e.enrolled_on::text AS enrolled_on,
       COUNT(*) OVER (ORDER BY e.enrolled_on, e.id) AS signups_so_far
FROM enrollment e
JOIN student s ON s.id = e.student_id
WHERE e.offering_id = 1
ORDER BY e.enrolled_on, e.id;
studentenrolled_onsignups_so_far
Kenji Park2025-09-161
Lucas Okafor2025-09-162
Nikolai Haddad2025-09-173
Omar Nguyen2025-09-184
Ayesha Kowalski2025-09-205
Diego Haddad2025-09-206
Diego Rossi2025-09-217
Liam Johansson2025-09-218

Replace COUNT(*) with SUM(amount_column) and you have a classic running total, such as sales so far this month. The e.id in the ORDER BY breaks ties between students who signed up on the same day.

What does OVER contain?

PartWhat it doesRequired?
PARTITION BYSplits the rows into groupsNo (without it, the whole result is one window)
ORDER BYSorts rows inside each windowNeeded for ranking functions and running totals

Common mistakes

  • Using a window function in WHERE: calculate it in a subquery or CTE, then filter outside.
  • Forgetting PARTITION BY: the numbers run across the whole result instead of restarting in each group.
  • Mixing up ROW_NUMBER and RANK: the results differ as soon as two rows tie.

What to read next

  • [Window functions: ranking](/learn/sql/window-functions-ranking) and [Window functions 2](/learn/sql/window-functions-2): practice ranking, group comparisons and running totals in the browser.
  • [SQL CTE and the WITH clause](/blog/sql-cte-with-clause): split a long query into named steps.
  • [SQL for beginners: the full course](/courses/sql)
  • #sql
  • #window functions
  • #row_number
  • #over
All posts