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_id | avg_midterm |
|---|---|
| 3 | 78.0 |
| 4 | 86.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;
| student | offering_id | midterm | offering_avg | diff |
|---|---|---|---|---|
| Kenji Park | 3 | 88 | 78.0 | 10.0 |
| Lucas Okafor | 3 | 75 | 78.0 | -3.0 |
| Ayesha Kowalski | 3 | 71 | 78.0 | -7.0 |
| Liam Johansson | 4 | 95 | 86.8 | 8.2 |
| Diego Haddad | 4 | 87 | 86.8 | 0.2 |
| Omar Nguyen | 4 | 86 | 86.8 | -0.8 |
| Diego Rossi | 4 | 85 | 86.8 | -1.8 |
| Ayesha Kowalski | 4 | 81 | 86.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;
| student | offering_id | midterm | place |
|---|---|---|---|
| Kenji Park | 3 | 88 | 1 |
| Lucas Okafor | 3 | 75 | 2 |
| Ayesha Kowalski | 3 | 71 | 3 |
| Liam Johansson | 4 | 95 | 1 |
| Diego Haddad | 4 | 87 | 2 |
| Omar Nguyen | 4 | 86 | 3 |
| Diego Rossi | 4 | 85 | 4 |
| Ayesha Kowalski | 4 | 81 | 5 |
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 WHERECalculate 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;
| student | offering_id | midterm | place |
|---|---|---|---|
| Kenji Park | 3 | 88 | 1 |
| Liam Johansson | 4 | 95 | 1 |
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;
| student | midterm | row_number | rank | dense_rank |
|---|---|---|---|---|
| Diego Rossi | 95 | 1 | 1 | 1 |
| Liam Johansson | 93 | 2 | 2 | 2 |
| Kenji Park | 89 | 3 | 3 | 3 |
| Diego Haddad | 78 | 4 | 4 | 4 |
| Ayesha Kowalski | 70 | 5 | 5 | 5 |
| Lucas Okafor | 70 | 6 | 5 | 5 |
| Omar Nguyen | 68 | 7 | 7 | 6 |
| Nikolai Haddad | 54 | 8 | 8 | 7 |
ROW_NUMBERalways gives different numbers, even for a tie. Which tied row gets 5 is arbitrary, so add a second column to theORDER BYif you need a stable result.RANKgives tied rows the same number, then skips: 5, 5, 7.DENSE_RANKgives 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;
| student | enrolled_on | signups_so_far |
|---|---|---|
| Kenji Park | 2025-09-16 | 1 |
| Lucas Okafor | 2025-09-16 | 2 |
| Nikolai Haddad | 2025-09-17 | 3 |
| Omar Nguyen | 2025-09-18 | 4 |
| Ayesha Kowalski | 2025-09-20 | 5 |
| Diego Haddad | 2025-09-20 | 6 |
| Diego Rossi | 2025-09-21 | 7 |
| Liam Johansson | 2025-09-21 | 8 |
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?
| Part | What it does | Required? |
|---|---|---|
PARTITION BY | Splits the rows into groups | No (without it, the whole result is one window) |
ORDER BY | Sorts rows inside each window | Needed 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