SQL LEARNING PLATFORM
The feature that separates intermediate from advanced SQL.
Learning Blocks
Interactive Queries
Concepts you'll master
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.
function_name() OVER (
PARTITION BY column -- divide rows into groups (optional)
ORDER BY column -- order within each group
ROWS/RANGE ... -- window frame (advanced, optional)
)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;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;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;SELECT order_id, order_date, total,
SUM(total) OVER (ORDER BY order_date) AS running_total
FROM orders;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.
Practice your SQL skills