Module 4 · Relating tables

Combine tables and keep control of the result rows

By the end you will be able to add united data without counting the same amount several times.

About 18 min

0 of 52 lessons

Before you start, you need

Keep all rows with LEFT JOIN · Filter groups with HAVING

Summarise without multiplying a measure

Joining tables can repeat a figure without there being any more money. Picture an order of 100 with three different products: when you join the order to its three lines, the total of 100 appears three times. If you add it up, you get 300 by mistake.

Decide what each row represents before adding. If you want the amount per customer, join customers and orders: each order must contribute its total only once. To calculate sales per product, use the quantities and prices on the lines.

In the example we keep every customer and add up their non-cancelled orders. GROUP BY c.id, c.name produces one row per customer. Using their id avoids mixing up people who share a name.

If a customer has no matching orders, SUM returns NULL. COALESCE lets you show a fallback: COALESCE(SUM(p.total), 0) returns the sum if there is one, and 0 if not. Do not use DISTINCT to hide repeated amounts: check which tables the question really needed.

Minimal syntax
SELECT key, SUM(measure)
FROM ...
GROUP BY key;

See it in action

What do each customer's non-cancelled orders add up to, including customers with none?

SQL
SELECT c.name,
       COALESCE(SUM(p.total), 0) AS total
FROM customers AS c
LEFT JOIN orders AS p
  ON c.id = p.customer_id
 AND p.status <> 'cancelled'
GROUP BY c.id, c.name;
Example result: 8 of 12 rows
nametotal
Amy Wilson377.4
Liam Martin253.4
Megan Reed215.9
David Clark69.9
Sara Green1112
Peter Hill0
Laura Ward148.9
Hugh Miller268
  1. Every customer is kept and their valid orders are looked up. The intermediate output has one row per customer-order pair.
  2. GROUP BY c.id, c.name produces one row per customer. SUM(p.total) adds each order once.
  3. If there are no matching orders, the sum is NULL. COALESCE(..., 0) shows 0 in that case: it returns the first value that is present.
  4. Do not join the order lines if you are going to add up the header total. Each line would repeat that figure and could inflate it.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Add up orders per customer

Not started

Write a query that returns every customer and their non-cancelled total; use 0 when they have no valid orders.

Expected result: 12 rows with the columns name, total, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Check the detail before the summary

Not started

Show order_id, the product name and the quantity for each line of order_items. Join order_items to products.

Expected result: 27 rows with the columns order_id, name, quantity, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Key takeaways

  • Define what a row represents before and after joining.
  • A join can repeat measures from the original table.
  • Group at the level the question asks for.
  • DISTINCT does not fix a multiplied sum.

Next: Practice: choose and check a JOIN