SQL guides

Beginner SQL project: analyse sales step by step

A shop manager asks which city generates the most sales revenue. Before building a report, define the scope, what each row represents and how to check the total.

This free example uses six fictional sales in euros. Each row is a completed sale; cancellations, refunds and taxes are outside its scope. It introduces the method used in the guided project, which has its own dataset.

1. Understand the data before the query

sale_id identifies a sale; city gives its location; channel distinguishes store from online sales; amount is the value in euros. A unique identifier lets you check that no sale is counted twice.

Fictional example data — sales (EUR)
sale_idcitychannelamount
1Parisstore120
2Lyononline80
3Parisonline180
4Lillestore50
5Lyonstore100
6Lilleonline70

2. Turn the question into steps

  1. Start from sales: each row represents a sale.
  2. Group by city to get one row per city.
  3. Add amount with SUM to calculate revenue.
  4. Sort from the highest revenue to the lowest.

3. Run your first SQL analysis

Dialect: PostgreSQL. SELECT, SUM, GROUP BY and ORDER BY are widely shared across SQL engines.

Query to try
SELECT city, SUM(amount) AS revenue
FROM sales
GROUP BY city
ORDER BY revenue DESC, city;

Browser-based SQL environment

3. Run your first SQL analysis

Run a read-only query on this exercise's fictional data. Nothing is sent to SQL.tn.

Local and private
Shortcut: Ctrl/⌘ + Enter
The SQL environment will be prepared when you first run the query.

4. Check the result before drawing conclusions

Paris totals €300, Lyon €180 and Lille €120. Together they make €600, matching the six sales. Each city has two sales: COUNT(*) would measure their number, not revenue.

5. Extend your analysis

Keep only the online channel. Place the filter before GROUP BY. The total should become €330: Paris €180, Lyon €80 and Lille €70. Explain how your scope changed.

A hint

Add WHERE channel = 'online' between FROM sales and GROUP BY city.

Continue in the guided project

You have practised reading, grouping and checking a result. Create a free account to work through the sales project missions and save progress. This sample does not automatically complete any project mission.