Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    SQLSQL

    First questions to the data

    • What are SQL and databases?
    • SELECT: choosing columns
    • WHERE: filtering rows
    • ORDER BY and LIMIT: sort and take the top

    Summarizing and grouping

    • Aggregates: COUNT, SUM, AVG
    • GROUP BY and HAVING
    • CASE, COALESCE and text functions

    Joining tables

    • Keys and relationships
    • INNER JOIN
    • LEFT JOIN: leave nobody out
    • Three-table joins and self joins
    • Subqueries and CTEs

    Building tables and changing data

    • CREATE TABLE and constraints
    • INSERT, UPDATE and DELETE
    • Transactions: all or nothing
    • SQL injection: the most dangerous mistake

    Professional level

    • Indexes: why is my query slow?
    • Window functions: rankings and totals without losing rows
    • Designing tables: normalization
    • Project: analytics for a learning platform
    SQL

    Professional level · Lesson 18 of 20

    Window functions: rankings and totals without losing rows

    ROW_NUMBER, RANK, PARTITION BY and running totals.

    12 minutes2 games

    GROUP BY squashes each group into one row. Sometimes you want every row, each with something computed from other rows: its rank, its city's average, the running total. That's what window functions do, and their sign is the word OVER.

    SQL
    SELECT name,
           xp,
           RANK() OVER (ORDER BY xp DESC) AS place,
           ROUND(AVG(xp) OVER ()) AS overall_avg
    FROM students
    WHERE xp IS NOT NULL
    ORDER BY place;

    OVER (ORDER BY xp DESC) means "sort all rows by XP and give each its position". An empty OVER () means "the window is every row". Notice all 7 rows are still there; nothing was squashed.

    PARTITION BY: per group

    SQL
    SELECT city,
           name,
           xp,
           ROW_NUMBER() OVER (PARTITION BY city ORDER BY xp DESC) AS rank_in_city
    FROM students
    ORDER BY city, rank_in_city;
    xpROW_NUMBERRANKDENSE_RANK

    500

    1

    1

    1

    400

    2

    2

    2

    400

    3

    2

    2

    300

    4

    4

    3

    The difference shows on ties: ROW_NUMBER never repeats, RANK repeats and leaves a gap, DENSE_RANK repeats without a gap.

    Running totals

    SQL
    -- How many enrollments had we reached by each date?
    SELECT enrolled_at,
           COUNT(*) OVER (ORDER BY enrolled_at) AS total_so_far
    FROM enrollments
    ORDER BY enrolled_at;

    Important

    Window functions are computed after WHERE, GROUP BY and HAVING, so you can't use them in WHERE. To filter on them (like "the top student per city"), compute them in a CTE or subquery and filter outside.

    What's the output?

    SELECT name,
           ROW_NUMBER() OVER (ORDER BY xp DESC) AS n
    FROM students
    WHERE city = 'Cairo'
    ORDER BY n;

    What will this code print?

    Interview question

    How do GROUP BY and window functions differ? And ROW_NUMBER, RANK and DENSE_RANK?

    Exercise

    Get the student with the highest XP in each city: city, name and xp, ordered by city.

    Expected result:
    city | name | xp
    Alexandria | Karim | 150
    Cairo | Laila | 320
    Giza | Nour | 410
    Mansoura | Hana | 60
    SQL
    WITH ranked AS (
      SELECT city, name, xp,
             ROW_NUMBER() OVER (PARTITION BY city ORDER BY xp DESC) AS r
      FROM students
    )
    SELECT
    

    Learn more from

    • PostgreSQL docs: Window functions tutorialpostgresql.org
    • SQLite: Window functionssqlite.org
    • Mode: SQL window functionsmode.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousIndexes: why is my query slow?NextDesigning tables: normalization
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.