26. Aggregate before you join Solved
PerformanceHardjoin orderpre-aggregationquery plan
For orders placed in Q4 2024 (October to December, any status), return per product: name, units and revenue. Sort by revenue descending, then name. Constraint: sum the order lines per product before joining products, so the join sees one row per product instead of one per order line.
Your answer returns 10 rows with the columns name, units, revenue. Order matters.
The data
The store from the DuckDB playground, running in your browser with DuckDB. Prices are in EUR; the data covers 2024.
customers20 rows
id | INTEGER primary key |
name | VARCHAR |
country | VARCHAR |
currency | VARCHAR EUR, GBP, USD or JPY |
signed_up | DATE |
tags | VARCHAR[] a list: vip, newsletter, b2b, returns-often |
products10 rows
id | INTEGER primary key |
name | VARCHAR |
category | VARCHAR |
price | DECIMAL(10,2) EUR |
specs | STRUCT weight_g INTEGER, color VARCHAR, sizes VARCHAR[] |
orders300 rows
id | INTEGER primary key |
customer_id | INTEGER → customers.id |
placed_at | TIMESTAMP 2024 |
status | ENUM placed, paid, shipped, delivered, returned |
currency | VARCHAR the customer's currency |
order_items576 rows
order_id | INTEGER → orders.id |
product_id | INTEGER → products.id |
quantity | INTEGER |
unit_price | DECIMAL(10,2) EUR |
web_events20,000 rows
event_id | BIGINT |
ts | TIMESTAMP |
session_id | VARCHAR |
customer_id | INTEGER NULL for anonymous visitors |
event_type | VARCHAR page_view, search, add_to_cart, checkout |
page | STRUCT path, referrer |
device | STRUCT browser, mobile BOOLEAN, screen STRUCT(w, h) |
product_ids | INTEGER[] |
load_ms | INTEGER |
fx_rates720 rows
currency | VARCHAR USD, GBP, JPY |
valid_from | TIMESTAMP |
per_eur | DOUBLE units of currency per 1 EUR |
Hints
order_items (joined to the Q4 orders) by product_id in a CTE, then join products.Common traps
- Joining products first and grouping by name makes the join carry every line.
Solution
WITH per_product AS (
SELECT oi.product_id, sum(oi.quantity) AS units, sum(oi.quantity * oi.unit_price) AS revenue
FROM order_items oi JOIN orders o ON o.id = oi.order_id
WHERE o.placed_at >= TIMESTAMP '2024-10-01' AND o.placed_at < TIMESTAMP '2025-01-01'
GROUP BY oi.product_id
)
SELECT p.name, pp.units, pp.revenue
FROM per_product pp JOIN products p ON p.id = pp.product_id
ORDER BY pp.revenue DESC, p.name
Why it is fast
The slower version returns exactly the same rows:
SELECT p.name, sum(oi.quantity) AS units, sum(oi.quantity * oi.unit_price) AS revenue
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
JOIN products p ON p.id = oi.product_id
WHERE o.placed_at >= TIMESTAMP '2024-10-01' AND o.placed_at < TIMESTAMP '2025-01-01'
GROUP BY p.name
ORDER BY revenue DESC, p.name
How this is graded: on DuckDB's physical plan of your final statement (EXPLAIN (FORMAT JSON)): order lines are aggregated before products is joined.
Free for personal practice. Please don't republish the questions or solutions. See the Playground License Terms.