DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 22

Window Functions

The feature that separates intermediate from advanced SQL.

IN THIS CHAPTER

  • How window functions differ from GROUP BY
  • ROW_NUMBER, RANK, DENSE_RANK
  • The Top-N-per-group pattern — used everywhere
  • Running totals with SUM() OVER
  • LAG and LEAD — looking backward and forward
  • NTILE for percentile buckets

🌟 Think of it this way: GROUP BY puts everyone in separate rooms and gives you a summary per room. Window functions keep everyone in the same room, but give each person their own name tag showing their rank, their group's average, and how they compare. You get the detail AND the summary.

Syntax

function_name() OVER (
    PARTITION BY column   -- divide rows into groups (optional)
    ORDER BY column       -- order within each group
    ROWS/RANGE ...        -- window frame (advanced, optional)
)

18.1 - ROW_NUMBER per Department

SQL

SELECT name, department, salary,
    ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC)
AS rank_in_dept
FROM employees
ORDER BY department, rank_in_dept;

18.2 - Top Earner per Department

The Top-N-per-group pattern — learn this by heart

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

18.2 - Top Earner per Department

The Top-N-per-group pattern — learn this by heart

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

18.3 - RANK vs DENSE_RANK

FunctionTie behaviourSequence: 100, 90, 90, 80
ROW_NUMBER()Arbitary1, 2, 3, 4
RANK()Same rank, skip next1, 2, 2, 4 (3 is skipped)
DENSE_RANK()Same rank, no skip1, 2, 2, 3 (consecutive)

18.4 - Running Total

SQL

SELECT order_id, order_date, total,
    SUM(total) OVER (ORDER BY order_date) AS running_total
FROM orders;

18.5 - LAG & LEAD

Each order vs previous and next order

SELECT order_id, total,
    LAG(total) OVER (ORDER BY order_date) AS prev_total,
    LEAD(total) OVER (ORDER BY order_date) AS next_total
FROM orders;

💡 Engineering Insight: Window functions were designed specifically for analytics workloads. Netflix uses them for viewing streak calculations, Uber for surge pricing windows, Swiggy for peak-hour analysis. Every data engineering role at a FAANG-level company expects fluency with window functions.

Interview Tip: 'Write a query to find the second highest salary in each department' is a top-5 SQL interview question at Amazon, Microsoft and Google. The answer: ROW_NUMBER() or DENSE_RANK() inside a CTE.

Exercise 👇

Exercise:

Tasks

1.👉Assign a row number to each employee based on salary (highest first).
2.Rank employees based on salary (highest first).
3.Assign row numbers within each department based on salary (highest first).
4.Rank employees within each department based on salary.
5.Show each employee's salary along with the average salary of all employees.
Stuck? Read this task's