Code24Lab
Read first

The SQL

SELECT c.customer_id, c.name,
       SUM(o.amount) AS revenue
FROM orders AS o
JOIN customers AS c USING (customer_id)
GROUP BY c.customer_id, c.name
ORDER BY revenue DESC;

-- variant with a filter:
SELECT SUM(o.amount)
FROM orders AS o
JOIN customers AS c USING (customer_id)
WHERE c.city = 'Basel';
`USING (customer_id)` only works when both tables call the key exactly the same. Otherwise write `ON a.x = b.y`.
Loading the lab…