Sub topic 13
When to Use a Window Function Instead
Assume you know basic SELECT, FROM, WHERE, ORDER BY, GROUP BY, and aggregate functions. You should also understand NULL and basic joins. This chapter compares grouped aggregates with window functions.
The key difference
A grouped query combines several input rows into one output row for each group. A window function calculates across related rows but keeps the original rows in the result.
-- GROUP BY returns one row per customer
SELECT customer_id,
SUM(amount) AS customer_total
FROM orders
GROUP BY customer_id;This query hides the individual orders. The result contains only one summary row per customer.
-- The window function keeps every order
SELECT order_id,
customer_id,
amount,
SUM(amount) OVER (
PARTITION BY customer_id
) AS customer_total
FROM orders;PARTITION BY customer_id divides the rows into customer partitions. The sum is calculated separately in each partition, but every order remains visible.
Choosing between the two
Use GROUP BY when the report needs one row per group. Use a window function when the report needs detail and context together.
| Requirement | Better choice | Result |
|---|---|---|
| Total sales per customer | `GROUP BY` | One row per customer |
| Show each order beside the customer total | Window function | One row per order |
| Keep only customers whose total exceeds 1,000 | `GROUP BY` with `HAVING` | Filtered customer groups |
| Show each order from customers whose total exceeds 1,000 | Window function in a subquery | Detailed rows for qualifying customers |
A window function does not replace GROUP BY in every query. It solves a different shape of problem.
Ordering and running calculations
Add ORDER BY inside OVER when the calculation depends on row order. For example, this produces a running total for each customer:
SELECT order_id,
customer_id,
amount,
SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY order_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_customer_total
FROM orders
ORDER BY customer_id, order_id;The window ORDER BY controls the calculation. The final query ORDER BY controls how the result is displayed. They are separate clauses.
Other useful window functions include ROW_NUMBER, RANK, LAG, and LEAD. For example, ROW_NUMBER can identify the most expensive order for each customer:
SELECT order_id,
customer_id,
amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY amount DESC, order_id
) AS customer_rank
FROM orders;To return only the top order, place this query in a subquery and filter the calculated rank outside it. A window function is evaluated after WHERE, so its alias usually cannot be used directly in the same query's WHERE clause.
A practical decision rule
Ask what one output row represents:
- If it represents a group, start with
GROUP BY. - If it represents an original row with extra comparison or summary data, use a window function.
- If you need both, aggregate in a subquery and join the result, or use a window function where it preserves the required detail.
Exercise
You need each order and the average order amount for that order's customer. Should you use GROUP BY or a window function?
Show answer
Use a window function: AVG(amount) OVER (PARTITION BY customer_id). GROUP BY would remove the individual order rows.