4. Data engineering patterns
4.3 Window functions: ROW_NUMBER and the latest row per group
Highlights & notes
Select lesson text to highlight it, or tap Add note.
Bookmarks
Bookmark a lesson to see it here.
GROUP BY squeezes each group into one row. A window function does a calculation across a group but keeps every row. It is the most asked topic in data engineering interviews.
ROW_NUMBER()→ numbers the rows 1, 2, 3...OVER ( ... )→ marks it as a window functionPARTITION BY customer_id→ restart the numbering for each customerORDER BY order_date DESC→ number the newest row as 1
Keep the rows where the number is 1 and you have the latest row per customer. This is how duplicates are removed from a table that receives the same record many times.
ROW_NUMBER vs RANK vs DENSE_RANK when two rows tie:
ROW_NUMBER→ 1, 2, 3 (always different)RANK→ 1, 1, 3 (a gap after the tie)DENSE_RANK→ 1, 1, 2 (no gap)
You cannot write WHERE rn = 1 in the same SELECT that creates rn, because WHERE runs before window functions. Create rn in a CTE, then filter it in the next step.
How it fits together
- Split rows by customer
- Number them, newest = 1
- Keep rows where rn = 1
Watch the query run
SQL
WITH ranked AS ( SELECT customer_id, product, order_date, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY order_date DESC ) AS rn FROM orders)SELECT customer_id, product, order_dateFROM rankedWHERE rn = 1ORDER BY customer_id;
Rows at this step
customer_id | product | order_date ------------+----------+----------- 1 | Monitor | 2026-01-20 2 | Laptop | 2026-01-15 3 | Keyboard | 2026-01-22 4 | Mouse | 2026-01-18
Practice in SQL ✓ Solved
Show each customer's most expensive order: customer_id, product and amount. Ignore orders with no amount. Sort by customer_id.
Tables you can query: customers, orders
customers id | name | city ---+-------+------ 1 | Asha | Noida 2 | Ravi | Delhi 3 | Meera | Pune 4 | Kabir | Noida 5 | Zoya | NULL orders id | customer_id | product | amount | status | order_date ----+-------------+----------+--------+-----------+----------- 101 | 1 | Laptop | 55000 | delivered | 2026-01-05 102 | 2 | Mouse | 700 | delivered | 2026-01-06 103 | 1 | Keyboard | 1500 | delivered | 2026-01-09 104 | 3 | Monitor | 12000 | cancelled | 2026-01-12 105 | 2 | Laptop | 52000 | delivered | 2026-01-15 106 | 4 | Mouse | 650 | pending | 2026-01-18 107 | 1 | Monitor | 11500 | delivered | 2026-01-20 108 | 3 | Keyboard | NULL | pending | 2026-01-22
Expected output
customer_id | product | amount ------------+---------+------- 1 | Laptop | 55000 2 | Laptop | 52000 3 | Monitor | 12000 4 | Mouse | 650
Reference solution
WITH ranked AS (
SELECT customer_id, product, amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY amount DESC
) AS rn
FROM orders
WHERE amount IS NOT NULL
)
SELECT customer_id, product, amount
FROM ranked
WHERE rn = 1
ORDER BY customer_id;
Question 1 · Easy
What does PARTITION BY customer_id do in a window function?
Question 2 · Medium
How is a window function different from GROUP BY?
Question 3 · Hard
A table gets a new copy of a customer record every day. How do you keep only the latest copy of each customer?
Key takeaways
A window function calculates across a group but keeps every row. ROW_NUMBER with PARTITION BY, then rn = 1, gives the latest row per group.
Save your progress
Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.
Start free Log in