Skip to main content
Xenors Xenors
XENORS
Home › Data Science › windowed analysis: OVER, PARTITION BY & Ranking
Module lesson · 19 of 45

SQL Window Functions: OVER, PARTITION BY & Ranking

Learn windowed analysis with OVER, PARTITION BY, ORDER BY, ROW_NUMBER, RANK, LAG and running totals.

By Ashok Kumar YadavHands-on examples + practiceDataset + notebook included
SQL Window Functions: OVER, PARTITION BY & Ranking

Learn windowed analysis with OVER, PARTITION BY, ORDER BY, ROW_NUMBER, RANK, LAG and running totals. Work through the windowed analysis examples in order, then change one assumption and explain whether your interpretation still holds.

Editorial note: Publication metadata for SQL Window Functions is intentionally unset until this URL is actually released; the lesson already includes its own examples, CSV and notebook.

Start with the table grain before writing windowed analysis Tutorial

To identify each customer’s latest order while keeping order details, assign ROW_NUMBER within each customer ordered by date descending, then keep row number 1.

  • Window functions calculate across related rows without collapsing them into one row per group.
  • PARTITION BY defines independent groups, while ORDER BY inside OVER defines sequence for ranking or cumulative calculations.
  • ROW_NUMBER, RANK and DENSE_RANK handle ties differently and can change downstream filtering.
  • Window frames matter for rolling sums and averages; the default frame may not match the business question.

Work through a query with row-count checks

For windowed analysis, predict one visible result before each snippet runs—a row count, shape, statistic, ranking or error pattern—then compare it with the printed checkpoint.

Example 1 · Baseline example
-- windowed analysis · example 1
WITH ranked AS (
  SELECT o.*,
         ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
  FROM orders o
)
SELECT * FROM ranked WHERE rn = 1;
Output / checkpoint
windowed analysis checkpoint 1: Run the cell and inspect the printed values before changing anything.
Example 2 · Confirm table grain
-- windowed analysis · example 2
SELECT COUNT(*) AS rows, COUNT(DISTINCT customer_id) AS customers
FROM practice_orders;
Output / checkpoint
windowed analysis checkpoint 2: rows | customers
30   | 12
Example 3 · Inspect duplicate keys
-- windowed analysis · example 3
SELECT customer_id, COUNT(*) AS n
FROM practice_orders
GROUP BY customer_id
HAVING COUNT(*) > 1
ORDER BY n DESC;
Output / checkpoint
windowed analysis checkpoint 3: Customers with more than one row are listed before any join or aggregation.
Example 4 · Validate a grouped result
-- windowed analysis · example 4
SELECT region, COUNT(*) AS orders, ROUND(SUM(amount),2) AS revenue
FROM practice_orders
GROUP BY region
ORDER BY revenue DESC;
Output / checkpoint
windowed analysis checkpoint 4: region | orders | revenue
West   | ...    | ...
Example 5 · Compare detail and aggregate totals
-- windowed analysis · example 5
WITH x AS (
  SELECT amount FROM practice_orders WHERE amount IS NOT NULL
)
SELECT ROUND(SUM(amount),2) AS detail_total,
       ROUND((SELECT SUM(amount) FROM x),2) AS check_total
FROM x;
Output / checkpoint
windowed analysis checkpoint 5: detail_total and check_total should match exactly.

Test duplicates, NULLs and unmatched keys

Verification habit

ROW_NUMBER, RANK and DENSE_RANK handle ties differently and can change downstream filtering.

Failure mode to watch

Window frames matter for rolling sums and averages; the default frame may not match the business question.

Self-check with answers

For windowed analysis, what result should you be able to explain without the code?

Window functions calculate across related rows without collapsing them into one row per group.

Which assumption matters most in this windowed analysis exercise?

ROW_NUMBER, RANK and DENSE_RANK handle ties differently and can change downstream filtering.

What failure mode would make this windowed analysis result untrustworthy?

Window frames matter for rolling sums and averages; the default frame may not match the business question.

Practise with a reproducible SQLite-style dataset

Try it yourself workspace

Edit this windowed analysis starter, copy it, and run it in the downloadable notebook or your local Jupyter environment.

Questions specific to this lesson

When should I use windowed analysis in an analysis?

Window functions calculate across related rows without collapsing them into one row per group.

What row-count check should I do before trusting the result?

ROW_NUMBER, RANK and DENSE_RANK handle ties differently and can change downstream filtering.

What is the most common beginner mistake here?

Window frames matter for rolling sums and averages; the default frame may not match the business question.

How can I practise this without a production database?

Use the CSV and notebook in this lesson, load the file into SQLite or pandas, predict the row count first, then compare your result with a simpler query.

Primary references and further reading

For windowed analysis, use these primary references when exact syntax, defaults or formal definitions matter beyond the examples on this page.

AK
Written by Ashok Kumar Yadav

Ashok Kumar Yadav writes Xenors tutorials on applied AI, programming and data workflows. This windowed analysis lesson is designed as a reproducible learning exercise rather than a keyword-first article.

GitHub · LinkedIn

Continue exploring