EVIKA.ACADEMY›SQL Tutorial
SQL Tutorial›Window Functions

SQL Window Functions

Chapter 28 of 40 · Estimated read time: 10 min

Window functions perform calculations across a set of rows related to the current row — without collapsing those rows the way GROUP BY does. Each row keeps its own output line; the window function adds an extra column showing the computed value for that row's "window" of data.

GROUP BY aggregate
Collapses rows — 7 order rows become 3 category rows. Original row detail is gone.
SELECT category, SUM(price)
FROM products
GROUP BY category;
-- 3 rows returned
Window function
Keeps all rows — adds the aggregate as an extra column alongside each row's own data.
SELECT category, price,
  SUM(price) OVER (PARTITION BY category)
FROM products;
-- 7 rows returned
function_name() OVER ( PARTITION BY column -- optional: split rows into groups ORDER BY column -- optional: define row order within window ROWS/RANGE BETWEEN … -- optional: define the frame )

Example 1 — Category Total Alongside Each Row

SELECT
  product_name,
  category,
  price,
  SUM(price) OVER (PARTITION BY category) AS category_total,
  ROUND(price * 100.0 / SUM(price) OVER (PARTITION BY category), 1) AS pct_of_category
FROM products
ORDER BY category, price;
Every row shows its own price AND the full category total — rows are not collapsed
product_namecategorypricecategory_totalpct_of_category
HDMI CableElectronics299269711.1
Wireless MouseElectronics899269733.3
USB-C HubElectronics1499269755.6
Desk LampFurniture11991019811.8
Ergonomic ChairFurniture89991019888.2
Ballpoint PensStationery4914833.1
Notebook A5Stationery9914866.9

Example 2 — Running Total of Orders Over Time

SELECT
  order_id,
  order_date,
  total_amount,
  SUM(total_amount) OVER (ORDER BY order_date) AS running_total
FROM orders
ORDER BY order_date;
Running total grows with each order — no GROUP BY needed
order_idorder_datetotal_amountrunning_total
10012026-10-01899899
10022026-10-0214992398
10032026-10-034952893
10042026-10-04899911892
10052026-10-05239814290
10062026-10-0629914589
10072026-10-0849015079
ORDER BY inside OVER() defines the order of the running calculation. Without PARTITION BY, the window spans the entire result set.

Example 3 — ROW_NUMBER and RANK

SELECT
  product_name,
  category,
  price,
  ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS row_num,
  RANK()       OVER (PARTITION BY category ORDER BY price DESC) AS price_rank
FROM products
ORDER BY category, price DESC;
ROW_NUMBER gives unique sequential numbers; RANK gives tied rows the same number
product_namecategorypricerow_numprice_rank
USB-C HubElectronics149911
Wireless MouseElectronics89922
HDMI CableElectronics29933
Ergonomic ChairFurniture899911
Desk LampFurniture119922
Notebook A5Stationery9911
Ballpoint PensStationery4922
ROW_NUMBER vs RANK vs DENSE_RANK:
• ROW_NUMBER — always unique: 1, 2, 3, 4
• RANK — ties get the same rank, next rank skips: 1, 1, 3, 4
• DENSE_RANK — ties get the same rank, no skipping: 1, 1, 2, 3

Example 4 — LAG and LEAD (Access Previous / Next Row)

SELECT
  order_id,
  order_date,
  total_amount,
  LAG(total_amount)  OVER (ORDER BY order_date) AS prev_order,
  LEAD(total_amount) OVER (ORDER BY order_date) AS next_order,
  total_amount - LAG(total_amount) OVER (ORDER BY order_date) AS change_from_prev
FROM orders
ORDER BY order_date;
LAG looks back one row; LEAD looks forward one row; NULL at the boundary
order_idorder_datetotal_amountprev_ordernext_orderchange_from_prev
10012026-10-01899NULL1499NULL
10022026-10-021499899495600
10032026-10-0349514998999-1004
10042026-10-04899949523988504
10052026-10-0523988999299-6601
10062026-10-062992398490-2099
10072026-10-08490299NULL191

Example 5 — Top N Per Group (Classic Interview Question)

-- Top 1 most expensive product per category
WITH ranked AS (
  SELECT
    product_name,
    category,
    price,
    ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS rn
  FROM products
)
SELECT product_name, category, price
FROM ranked
WHERE rn = 1;
Most expensive product in each category — a CTE + ROW_NUMBER pattern
product_namecategoryprice
USB-C HubElectronics1499
Ergonomic ChairFurniture8999
Notebook A5Stationery99
Top-N per group using ROW_NUMBER + CTE is one of the most common SQL interview questions. Memorise this pattern: PARTITION BY the group, ORDER BY the value, filter WHERE rn <= N.
✏️ Practice Exercises
1. Add a column showing the running total of total_amount per customer (PARTITION BY customer_id, ORDER BY order_date).
2. Rank customers by their total spend using RANK(). Who is #1?
3. Using LAG, find orders where total_amount dropped compared to the previous order.
Show Answers
-- 1.
SELECT
  customer_id, order_id, order_date, total_amount,
  SUM(total_amount) OVER (
    PARTITION BY customer_id
    ORDER BY order_date
  ) AS running_total_per_customer
FROM orders;

-- 2.
SELECT
  customer_id,
  SUM(total_amount) AS total_spent,
  RANK() OVER (ORDER BY SUM(total_amount) DESC) AS spend_rank
FROM orders
GROUP BY customer_id;

-- 3.
SELECT order_id, order_date, total_amount,
  LAG(total_amount) OVER (ORDER BY order_date) AS prev_amount
FROM orders
WHERE total_amount < LAG(total_amount) OVER (ORDER BY order_date);
-- Note: use a CTE or subquery to reference the window result in WHERE