Summary - Window functions
- SQL course
- Advanced
- Lesson 18/81
- 5 min
Write a month-over-month report with ROW_NUMBER and LAG.
Summary - Window functions
Window functions compute without collapsing rows. ROW_NUMBER, RANK, LAG and PARTITION BY are common.
SELECT name, price,
RANK() OVER (ORDER BY price DESC) AS rk
FROM products;Summary window query.
name, price, RANK() OVER (ORDER BY price DESC) AS rk is selected from products. RANK() is a window function: it computes a value for each row over the window defined in OVER (…), without collapsing rows.
Practice Summary - Window functions
Summary: find each product's price rank with RANK() OVER (ORDER BY price DESC).
Starter code
-- summary window
SELECT name, price,
RANK()Sample solution
SELECT name, price,
RANK() OVER (ORDER BY price DESC) AS rk
FROM products;Expected output
name rk Laptop 1 Telefon 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What defines the window in a window function?
- WHERE
- OVER Correct answer
- FROM
- GROUP BY
Explanation: OVER defines the window.
2 Summary - Window functions: match the terms
| Term | Meaning |
|---|---|
ROW_NUMBER |
row number |
RANK |
rank |
LAG |
previous |
PARTITION BY |
group |