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;
namepricevariable_costunit_margin
U-lock 180 mm49.921.428.5
Folding lock69.933.536.4
Cable lock 120 cm19.914.95

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_idproduct_idmonthunitsrevenue
112026-0166525472.16
222026-0190512945.12
332026-0134517804.07
442026-0159528502.88
552026-0132519537.05
662026-0181015474.24
772026-0130017251.2
882026-0124510560.48
992026-0146511115.36
10102026-0156511878.56
11112026-0143012342.72
12122026-0111207418.88
1312026-0288033707.52
1422026-02121017307.84
1532026-0250526061.03
1642026-0269533293.28
1752026-0240024045.6
1862026-0290517289.12
1972026-0246026451.84
2082026-0233014224.32
2192026-0260014342.4
22102026-0285517975.52
23112026-0251014639.04
24122026-02151010002.24
2512026-03134551518.88
2622026-03173024745.92
2732026-0375538962.53
2842026-03117056047.68
2952026-0354032461.56
3062026-03158030184.32
3172026-0365037377.6
3282026-0350021552
3392026-0395022708.8
34102026-03117024598.08
35112026-0376021815.04
36122026-03210513943.52
3712026-04182569904.8
3822026-04206029466.24
3932026-0495049025.7
4042026-04125059880
4152026-0467040276.38
4262026-04167531999.2
4372026-0475543415.52
4482026-0454023276.16
4592026-04110526413.92
46102026-04142529959.2
47112026-04100528847.52
48122026-04297019673.28
4912026-05155059371.2
5022026-05214530682.08
5132026-0589546187.37
5242026-05136565388.96
5352026-0564538773.53
5462026-05179534291.68
5572026-0569539965.28
5682026-0555523922.72
5792026-05108525935.84
58102026-05132027751.68
59112026-0594527125.28
60122026-05279018480.96
6112026-06138553051.04
6222026-06162523244
6332026-0666534317.99
6442026-06105550538.72
6552026-0654532762.13
6662026-06136025981.44
6772026-0659534214.88
6882026-0639016810.56
6992026-0681519481.76
70102026-06119525123.68
71112026-0671520523.36
72122026-06204013512.96
products 12 rows, 5 columns
product_idnamegroup_idpricevariable_cost
1LED front light 70 lux139.917.2
2LED rear light114.96.1
3Rechargeable light set154.927.8
4U-lock 180 mm249.921.4
5Folding lock269.933.5
6Cable lock 120 cm219.914.9
7Pannier bag 20 l359.931.4
8Handlebar bag344.924.8
9Small saddle bag324.913.2
10Mini pump421.98.9
11Multi-tool 16 functions429.912.6
12Puncture repair kit46.92.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.

LayerCalculation
RevenueSUM(s.revenue)
cm1revenue minus variable costs
cm2cm1 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_idproduct_idmonthunitsrevenue
112026-0166525472.16
222026-0190512945.12
332026-0134517804.07
442026-0159528502.88
552026-0132519537.05
662026-0181015474.24
772026-0130017251.2
882026-0124510560.48
992026-0146511115.36
10102026-0156511878.56
11112026-0143012342.72
12122026-0111207418.88
1312026-0288033707.52
1422026-02121017307.84
1532026-0250526061.03
1642026-0269533293.28
1752026-0240024045.6
1862026-0290517289.12
1972026-0246026451.84
2082026-0233014224.32
2192026-0260014342.4
22102026-0285517975.52
23112026-0251014639.04
24122026-02151010002.24
2512026-03134551518.88
2622026-03173024745.92
2732026-0375538962.53
2842026-03117056047.68
2952026-0354032461.56
3062026-03158030184.32
3172026-0365037377.6
3282026-0350021552
3392026-0395022708.8
34102026-03117024598.08
35112026-0376021815.04
36122026-03210513943.52
3712026-04182569904.8
3822026-04206029466.24
3932026-0495049025.7
4042026-04125059880
4152026-0467040276.38
4262026-04167531999.2
4372026-0475543415.52
4482026-0454023276.16
4592026-04110526413.92
46102026-04142529959.2
47112026-04100528847.52
48122026-04297019673.28
4912026-05155059371.2
5022026-05214530682.08
5132026-0589546187.37
5242026-05136565388.96
5352026-0564538773.53
5462026-05179534291.68
5572026-0569539965.28
5682026-0555523922.72
5792026-05108525935.84
58102026-05132027751.68
59112026-0594527125.28
60122026-05279018480.96
6112026-06138553051.04
6222026-06162523244
6332026-0666534317.99
6442026-06105550538.72
6552026-0654532762.13
6662026-06136025981.44
6772026-0659534214.88
6882026-0639016810.56
6992026-0681519481.76
70102026-06119525123.68
71112026-0671520523.36
72122026-06204013512.96
products 12 rows, 5 columns
product_idnamegroup_idpricevariable_cost
1LED front light 70 lux139.917.2
2LED rear light114.96.1
3Rechargeable light set154.927.8
4U-lock 180 mm249.921.4
5Folding lock269.933.5
6Cable lock 120 cm219.914.9
7Pannier bag 20 l359.931.4
8Handlebar bag344.924.8
9Small saddle bag324.913.2
10Mini pump421.98.9
11Multi-tool 16 functions429.912.6
12Puncture repair kit46.92.1
product_groups 4 rows, 3 columns
group_idnamefixed_costs
1Lights124000
2Locks115000
3Bags205000
4Tools40000

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.

Next: case 3, receivables aging

Which invoices are still unpaid, which are overdue, and how old is the balance? With LEFT JOIN, date arithmetic and CASE.

Go to case 3

Back to the course overview.

Write to us

Choose language

Select your preferred language