Skip to course content
Free SQL course

SQL for Data Analysis and AI

Module 04 Activity

Scenario

You have been asked for completed revenue split by customer country, for a slide that will be shown to people who will add up your numbers.

Task

  1. Write a query producing, per country, the completed order count and completed revenue.
  2. Compute the overall completed totals separately.
  3. Check whether your per-country rows sum to those overall totals. If they do not, find out where the

difference went before changing anything.

  1. Decide how to present customers whose country is not recorded, and apply that decision in the query.
  2. Add a total row and confirm it reconciles.

Deliverable

A segment table with a visible total row, plus one sentence below it stating the treatment of missing countries.

Check your work

CountryCompleted ordersCompleted revenue
IN671₹18,20,744
GB187₹5,11,887
SG69₹1,88,437
Not recorded61₹1,64,837
Total988₹26,85,905

The counts sum to 988 and the revenue sums to ₹26,85,905 - both matching the overall completed figures.

The whole point of this activity is the fourth row. If you filtered missing countries out with WHERE country IS NOT NULL, your table shows 927 orders and ₹25,21,068, which does not reconcile to the completed total. Someone in the room will subtract and find the gap.

Use COALESCE(country, 'Not recorded') and keep the bucket visible. A segment worth ₹1.6 lakh across 61 orders is not noise - it is a data-quality finding that belongs in front of the audience, not hidden by a WHERE clause.

Common mistake

Grouping by a column and filtering on an aggregate are different operations. If you want to show only countries above a revenue threshold, that belongs in HAVING, applied after grouping - and if you do that, your parts will no longer sum to the whole, so you must say so explicitly.