Ad space (banner)
🗄️SQL Lessons
Lesson 11 / 50

Ranking Rows with Window Functions

This lesson covers ranking rows with window functions — a step beyond GROUP BY aggregation. It's for anyone searching "SQL window function RANK."

A window function like RANK() OVER (ORDER BY column DESC) computes a rank or running total per row, without collapsing rows together the way GROUP BY does — it's a more advanced aggregation technique.

The example uses RANK() OVER (ORDER BY score DESC) to compute each student's rank while keeping every original row intact — notice, unlike GROUP BY, each student's name and score still appear individually in the result.

A common early mistake is being confused by what goes inside the parentheses after OVER — that's where you specify what scope and order the calculation applies to, which looks intimidating at first, so it helps to start with the simplest possible form (just ORDER BY). The key difference from ordinary GROUP BY: it collapses rows into one per group, while a window function keeps every row and just adds "this row's rank" alongside it — perfect for leaderboards and running totals.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)