Case 2: contribution margin in SQL
Revenue on its own tells you little. Contribution margin shows what a product leaves over after its variable costs, and the next layer shows whether a whole product group covers the fixed costs it causes. You calculate both from the sales data.
In short: contribution margin is revenue minus variable costs, worked out on each sales row as s.revenue - s.units * p.variable_cost and then summed. The margin ratio is that margin as a percentage of revenue. Taking off the fixed costs a product group causes gives the margin after fixed costs, often called segment margin. Those fixed costs are stored once per group and must not be summed after the join.
The situation
Sales is planning next year's range, and there's a proposal on the table to expand the bags because they sell well. Before deciding, management wants to see what each product and each of the four product groups actually earned in the first half. The sales data has units and revenue per product and month, the product master has variable cost per unit, and FP&A has worked out the fixed costs that belong to each group.
The tables
- sales: one row per product and month with units and revenue. Revenue is already net of discounts.
- products: name, group_id, list price and variable_cost per unit (materials, packaging, freight).
- product_groups: name and fixed_costs for January to June, such as tooling, licences and product management, which would go away without the group.
A first look at the locks shows unit margin at list price:
SELECT name, price, variable_cost, ROUND(price - variable_cost, 2) AS unit_margin
FROM products
WHERE group_id = 2;
| name | price | variable_cost | unit_margin |
|---|---|---|---|
| U-lock 180 mm | 49.9 | 21.4 | 28.5 |
| Folding lock | 69.9 | 33.5 | 36.4 |
| Cable lock 120 cm | 19.9 | 14.9 | 5 |
That's only half the story. The folding lock sold on promotion all half-year, at 60.11 on average instead of 69.90. So you work with the actual revenue in sales, not units times price.
Task 1: margin and margin ratio per product
Task Sales want to know which products really make money in the first half-year. For each product show: product (name), revenue (sum of revenue in sales), variable_costs (units times variable cost per unit, added up), cm1 (revenue minus variable costs) and cm_ratio (cm1 as a percentage of revenue, rounded to one decimal place). Sorted by cm_ratio, highest first.
sales 72 rows, 5 columns
| sale_id | product_id | month | units | revenue |
|---|---|---|---|---|
| 1 | 1 | 2026-01 | 665 | 25472.16 |
| 2 | 2 | 2026-01 | 905 | 12945.12 |
| 3 | 3 | 2026-01 | 345 | 17804.07 |
| 4 | 4 | 2026-01 | 595 | 28502.88 |
| 5 | 5 | 2026-01 | 325 | 19537.05 |
| 6 | 6 | 2026-01 | 810 | 15474.24 |
| 7 | 7 | 2026-01 | 300 | 17251.2 |
| 8 | 8 | 2026-01 | 245 | 10560.48 |
| 9 | 9 | 2026-01 | 465 | 11115.36 |
| 10 | 10 | 2026-01 | 565 | 11878.56 |
| 11 | 11 | 2026-01 | 430 | 12342.72 |
| 12 | 12 | 2026-01 | 1120 | 7418.88 |
| 13 | 1 | 2026-02 | 880 | 33707.52 |
| 14 | 2 | 2026-02 | 1210 | 17307.84 |
| 15 | 3 | 2026-02 | 505 | 26061.03 |
| 16 | 4 | 2026-02 | 695 | 33293.28 |
| 17 | 5 | 2026-02 | 400 | 24045.6 |
| 18 | 6 | 2026-02 | 905 | 17289.12 |
| 19 | 7 | 2026-02 | 460 | 26451.84 |
| 20 | 8 | 2026-02 | 330 | 14224.32 |
| 21 | 9 | 2026-02 | 600 | 14342.4 |
| 22 | 10 | 2026-02 | 855 | 17975.52 |
| 23 | 11 | 2026-02 | 510 | 14639.04 |
| 24 | 12 | 2026-02 | 1510 | 10002.24 |
| 25 | 1 | 2026-03 | 1345 | 51518.88 |
| 26 | 2 | 2026-03 | 1730 | 24745.92 |
| 27 | 3 | 2026-03 | 755 | 38962.53 |
| 28 | 4 | 2026-03 | 1170 | 56047.68 |
| 29 | 5 | 2026-03 | 540 | 32461.56 |
| 30 | 6 | 2026-03 | 1580 | 30184.32 |
| 31 | 7 | 2026-03 | 650 | 37377.6 |
| 32 | 8 | 2026-03 | 500 | 21552 |
| 33 | 9 | 2026-03 | 950 | 22708.8 |
| 34 | 10 | 2026-03 | 1170 | 24598.08 |
| 35 | 11 | 2026-03 | 760 | 21815.04 |
| 36 | 12 | 2026-03 | 2105 | 13943.52 |
| 37 | 1 | 2026-04 | 1825 | 69904.8 |
| 38 | 2 | 2026-04 | 2060 | 29466.24 |
| 39 | 3 | 2026-04 | 950 | 49025.7 |
| 40 | 4 | 2026-04 | 1250 | 59880 |
| 41 | 5 | 2026-04 | 670 | 40276.38 |
| 42 | 6 | 2026-04 | 1675 | 31999.2 |
| 43 | 7 | 2026-04 | 755 | 43415.52 |
| 44 | 8 | 2026-04 | 540 | 23276.16 |
| 45 | 9 | 2026-04 | 1105 | 26413.92 |
| 46 | 10 | 2026-04 | 1425 | 29959.2 |
| 47 | 11 | 2026-04 | 1005 | 28847.52 |
| 48 | 12 | 2026-04 | 2970 | 19673.28 |
| 49 | 1 | 2026-05 | 1550 | 59371.2 |
| 50 | 2 | 2026-05 | 2145 | 30682.08 |
| 51 | 3 | 2026-05 | 895 | 46187.37 |
| 52 | 4 | 2026-05 | 1365 | 65388.96 |
| 53 | 5 | 2026-05 | 645 | 38773.53 |
| 54 | 6 | 2026-05 | 1795 | 34291.68 |
| 55 | 7 | 2026-05 | 695 | 39965.28 |
| 56 | 8 | 2026-05 | 555 | 23922.72 |
| 57 | 9 | 2026-05 | 1085 | 25935.84 |
| 58 | 10 | 2026-05 | 1320 | 27751.68 |
| 59 | 11 | 2026-05 | 945 | 27125.28 |
| 60 | 12 | 2026-05 | 2790 | 18480.96 |
| 61 | 1 | 2026-06 | 1385 | 53051.04 |
| 62 | 2 | 2026-06 | 1625 | 23244 |
| 63 | 3 | 2026-06 | 665 | 34317.99 |
| 64 | 4 | 2026-06 | 1055 | 50538.72 |
| 65 | 5 | 2026-06 | 545 | 32762.13 |
| 66 | 6 | 2026-06 | 1360 | 25981.44 |
| 67 | 7 | 2026-06 | 595 | 34214.88 |
| 68 | 8 | 2026-06 | 390 | 16810.56 |
| 69 | 9 | 2026-06 | 815 | 19481.76 |
| 70 | 10 | 2026-06 | 1195 | 25123.68 |
| 71 | 11 | 2026-06 | 715 | 20523.36 |
| 72 | 12 | 2026-06 | 2040 | 13512.96 |
products 12 rows, 5 columns
| product_id | name | group_id | price | variable_cost |
|---|---|---|---|---|
| 1 | LED front light 70 lux | 1 | 39.9 | 17.2 |
| 2 | LED rear light | 1 | 14.9 | 6.1 |
| 3 | Rechargeable light set | 1 | 54.9 | 27.8 |
| 4 | U-lock 180 mm | 2 | 49.9 | 21.4 |
| 5 | Folding lock | 2 | 69.9 | 33.5 |
| 6 | Cable lock 120 cm | 2 | 19.9 | 14.9 |
| 7 | Pannier bag 20 l | 3 | 59.9 | 31.4 |
| 8 | Handlebar bag | 3 | 44.9 | 24.8 |
| 9 | Small saddle bag | 3 | 24.9 | 13.2 |
| 10 | Mini pump | 4 | 21.9 | 8.9 |
| 11 | Multi-tool 16 functions | 4 | 29.9 | 12.6 |
| 12 | Puncture repair kit | 4 | 6.9 | 2.1 |
Step 1: join sales and products
Units are in sales, unit costs in products. A join on product_id brings them into one row. Each sales row finds exactly one product, so no extra rows appear.
Step 2: multiply first, then sum
The variable cost of one sales row is s.units * p.variable_cost, and that calculation goes inside the sum: SUM(s.units * p.variable_cost). After GROUP BY p.product_id it gives each product's variable costs for the half-year. Margin follows the same way, as SUM(s.revenue - s.units * p.variable_cost).
Step 3: ratio and sort order
The margin ratio relates margin to revenue: ROUND(100 * … / SUM(s.revenue), 1). Only the ratio makes big and small products comparable. Sorted by ratio, the puncture repair kit comes first and the cable lock last: plenty of revenue, but only about a fifth of it stays.
Task 2: margin after fixed costs per product group
A layered margin statement doesn't take fixed costs off in one lump. It takes them off where they arise. A product group's fixed costs exist only because the group exists, and what's left after them is the group's contribution to the rest of the company's overheads.
| Layer | Calculation |
|---|---|
| Revenue | SUM(s.revenue) |
| cm1 | revenue minus variable costs |
| cm2 | cm1 minus the product group's fixed costs |
Task Now one level further down the margin statement: for each product group show product_group (name), revenue, cm1, fixed_costs (the group's fixed costs for January to June from product_groups) and cm2 (cm1 minus fixed_costs). Sorted by cm2, highest first.
sales 72 rows, 5 columns
| sale_id | product_id | month | units | revenue |
|---|---|---|---|---|
| 1 | 1 | 2026-01 | 665 | 25472.16 |
| 2 | 2 | 2026-01 | 905 | 12945.12 |
| 3 | 3 | 2026-01 | 345 | 17804.07 |
| 4 | 4 | 2026-01 | 595 | 28502.88 |
| 5 | 5 | 2026-01 | 325 | 19537.05 |
| 6 | 6 | 2026-01 | 810 | 15474.24 |
| 7 | 7 | 2026-01 | 300 | 17251.2 |
| 8 | 8 | 2026-01 | 245 | 10560.48 |
| 9 | 9 | 2026-01 | 465 | 11115.36 |
| 10 | 10 | 2026-01 | 565 | 11878.56 |
| 11 | 11 | 2026-01 | 430 | 12342.72 |
| 12 | 12 | 2026-01 | 1120 | 7418.88 |
| 13 | 1 | 2026-02 | 880 | 33707.52 |
| 14 | 2 | 2026-02 | 1210 | 17307.84 |
| 15 | 3 | 2026-02 | 505 | 26061.03 |
| 16 | 4 | 2026-02 | 695 | 33293.28 |
| 17 | 5 | 2026-02 | 400 | 24045.6 |
| 18 | 6 | 2026-02 | 905 | 17289.12 |
| 19 | 7 | 2026-02 | 460 | 26451.84 |
| 20 | 8 | 2026-02 | 330 | 14224.32 |
| 21 | 9 | 2026-02 | 600 | 14342.4 |
| 22 | 10 | 2026-02 | 855 | 17975.52 |
| 23 | 11 | 2026-02 | 510 | 14639.04 |
| 24 | 12 | 2026-02 | 1510 | 10002.24 |
| 25 | 1 | 2026-03 | 1345 | 51518.88 |
| 26 | 2 | 2026-03 | 1730 | 24745.92 |
| 27 | 3 | 2026-03 | 755 | 38962.53 |
| 28 | 4 | 2026-03 | 1170 | 56047.68 |
| 29 | 5 | 2026-03 | 540 | 32461.56 |
| 30 | 6 | 2026-03 | 1580 | 30184.32 |
| 31 | 7 | 2026-03 | 650 | 37377.6 |
| 32 | 8 | 2026-03 | 500 | 21552 |
| 33 | 9 | 2026-03 | 950 | 22708.8 |
| 34 | 10 | 2026-03 | 1170 | 24598.08 |
| 35 | 11 | 2026-03 | 760 | 21815.04 |
| 36 | 12 | 2026-03 | 2105 | 13943.52 |
| 37 | 1 | 2026-04 | 1825 | 69904.8 |
| 38 | 2 | 2026-04 | 2060 | 29466.24 |
| 39 | 3 | 2026-04 | 950 | 49025.7 |
| 40 | 4 | 2026-04 | 1250 | 59880 |
| 41 | 5 | 2026-04 | 670 | 40276.38 |
| 42 | 6 | 2026-04 | 1675 | 31999.2 |
| 43 | 7 | 2026-04 | 755 | 43415.52 |
| 44 | 8 | 2026-04 | 540 | 23276.16 |
| 45 | 9 | 2026-04 | 1105 | 26413.92 |
| 46 | 10 | 2026-04 | 1425 | 29959.2 |
| 47 | 11 | 2026-04 | 1005 | 28847.52 |
| 48 | 12 | 2026-04 | 2970 | 19673.28 |
| 49 | 1 | 2026-05 | 1550 | 59371.2 |
| 50 | 2 | 2026-05 | 2145 | 30682.08 |
| 51 | 3 | 2026-05 | 895 | 46187.37 |
| 52 | 4 | 2026-05 | 1365 | 65388.96 |
| 53 | 5 | 2026-05 | 645 | 38773.53 |
| 54 | 6 | 2026-05 | 1795 | 34291.68 |
| 55 | 7 | 2026-05 | 695 | 39965.28 |
| 56 | 8 | 2026-05 | 555 | 23922.72 |
| 57 | 9 | 2026-05 | 1085 | 25935.84 |
| 58 | 10 | 2026-05 | 1320 | 27751.68 |
| 59 | 11 | 2026-05 | 945 | 27125.28 |
| 60 | 12 | 2026-05 | 2790 | 18480.96 |
| 61 | 1 | 2026-06 | 1385 | 53051.04 |
| 62 | 2 | 2026-06 | 1625 | 23244 |
| 63 | 3 | 2026-06 | 665 | 34317.99 |
| 64 | 4 | 2026-06 | 1055 | 50538.72 |
| 65 | 5 | 2026-06 | 545 | 32762.13 |
| 66 | 6 | 2026-06 | 1360 | 25981.44 |
| 67 | 7 | 2026-06 | 595 | 34214.88 |
| 68 | 8 | 2026-06 | 390 | 16810.56 |
| 69 | 9 | 2026-06 | 815 | 19481.76 |
| 70 | 10 | 2026-06 | 1195 | 25123.68 |
| 71 | 11 | 2026-06 | 715 | 20523.36 |
| 72 | 12 | 2026-06 | 2040 | 13512.96 |
products 12 rows, 5 columns
| product_id | name | group_id | price | variable_cost |
|---|---|---|---|---|
| 1 | LED front light 70 lux | 1 | 39.9 | 17.2 |
| 2 | LED rear light | 1 | 14.9 | 6.1 |
| 3 | Rechargeable light set | 1 | 54.9 | 27.8 |
| 4 | U-lock 180 mm | 2 | 49.9 | 21.4 |
| 5 | Folding lock | 2 | 69.9 | 33.5 |
| 6 | Cable lock 120 cm | 2 | 19.9 | 14.9 |
| 7 | Pannier bag 20 l | 3 | 59.9 | 31.4 |
| 8 | Handlebar bag | 3 | 44.9 | 24.8 |
| 9 | Small saddle bag | 3 | 24.9 | 13.2 |
| 10 | Mini pump | 4 | 21.9 | 8.9 |
| 11 | Multi-tool 16 functions | 4 | 29.9 | 12.6 |
| 12 | Puncture repair kit | 4 | 6.9 | 2.1 |
product_groups 4 rows, 3 columns
| group_id | name | fixed_costs |
|---|---|---|
| 1 | Lights | 124000 |
| 2 | Locks | 115000 |
| 3 | Bags | 205000 |
| 4 | Tools | 40000 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
The tricky column is fixed_costs. After joining sales, products and product_groups, every group sits on 18 rows: three products times six months. An aggregate function such as SUM would count its fixed costs 18 times. Because you group by g.group_id and the fixed costs are the same on every row of the group, you output g.fixed_costs directly. Another clean route is to work out cm1 per group in a CTE first and join product_groups afterwards.
The result answers management's question: bags have a decent cm1 but don't cover their own fixed costs. Expanding them only pays off if those fixed costs don't grow at the same pace, or prices go up.
Common mistakes
Summing unit costs without units
SUM(p.variable_cost) just adds the unit cost six times, once per monthly row. The exercise says: "Columns and row count match, but not every value."
Using the list price
SUM(s.units * p.price) is revenue before discounts. For the folding lock, sold at 14% off, that's about 16% too high, and every other product is off too.
Percentage without the 100
Without 100 * you get 0.6 instead of 55.1. Rounded to one decimal place almost nothing is left, and the exercise reports wrong values.
Summing the fixed costs
SUM(g.fixed_costs) counts the fixed costs as often as the group has rows, and every group's cm2 turns deeply negative.
Grouping by product instead of group
With GROUP BY p.product_id in task 2 you get 12 rows instead of 4, each carrying the group's full fixed costs. The exercise says: "Your query returns 12 rows. Wanted: 4 rows."
Check your result
- Task 1: revenue across all twelve products is 2,055,135.42. Puncture repair kit 68.3%, cable lock 22.0%.
- Task 2: Lights has the highest cm2 at 214,780.99; Bags end up at −14,218.36.
- The total cm1 in task 1 must equal the total cm1 in task 2. Only the grouping differs.
The same calculation with a third layer and a capacity bottleneck is in the Excel contribution margin case.
Back to the course overview.