Analyse sales data with SQL in the browser

A 45-minute SQL exercise on a small table of 30 orders. It runs in any browser, with no database to install and no account, for lessons on databases and business data.

Lesson plan45 minBachelor +2CC BY 4.0

What you will learn

  • Turn a business question into a SQL query
  • Filter rows with WHERE, LIKE and IN
  • Summarise data with GROUP BY, COUNT and SUM
  • Explain why HAVING filters groups and WHERE filters rows

Before you start: None. Learners should know what a table, a row and a column are.

What you need

  • A current browser on any device. The SQL Query Playground runs inside the page, so the data never leaves the device and nobody needs an account.

  • The built-in sample table. Click Try sample data and the table is loaded as data: 30 orders from fictional companies, with the columns order_id, date, customer, company, country, channel, product, units, unit_price, revenue and status.

Running the lesson

45 min
  1. 5 min

    Everyone opens the playground, loads the sample and runs SELECT * FROM data LIMIT 10. One row is one order.

  2. 25 min

    Learners work through the six tasks below in pairs. Each task names the SQL it practises.

  3. 10 min

    Compare queries. Different queries can give the same right answer, which is worth pointing out.

  4. 5 min

    Exit question: why does task 6 need HAVING and not WHERE?

Tasks

  1. Which orders came from Germany? Show the order number, customer, company and revenue. (WHERE)

  2. List every order with more than 400 in revenue, the largest first. (WHERE, ORDER BY)

  3. Which companies have "trading" or "supply" in their name? Each company once. (DISTINCT, LIKE)

  4. How many orders are paid, refunded and pending? (GROUP BY, COUNT)

  5. How much paid revenue did each sales channel bring in? (WHERE, GROUP BY, SUM)

  6. Which customers placed more than two orders? (GROUP BY, HAVING)

Show the solutions

These are the results on the built-in sample data.

1. Orders from Germany

Task 1
SELECT order_id, customer, company, revenue
FROM data
WHERE country = 'Germany'

6 orders, from Sam Reed (Northwind Trading) and Lena Fischer (Umbrella Retail).

2. Orders over 400

Task 2
SELECT order_id, company, revenue
FROM data
WHERE revenue > 400
ORDER BY revenue DESC

8 orders, from 798.00 (orders 1016 and 1026) down to 447.00.

3. Trading and supply companies

Task 3
SELECT DISTINCT company
FROM data
WHERE company LIKE '%trading%' OR company LIKE '%supply%'

Acme Supply Co and Northwind Trading. LIKE ignores upper and lower case here.

4. Orders by status

Task 4
SELECT status, COUNT(*) AS orders
FROM data
GROUP BY status

25 paid, 3 refunded and 2 pending.

5. Paid revenue by channel

Task 5
SELECT channel, ROUND(SUM(revenue), 2) AS revenue
FROM data
WHERE status = 'paid'
GROUP BY channel
ORDER BY revenue DESC

Direct 4563, Marketplace 2474 and Partner 2432.

6. Customers with more than two orders

Task 6
SELECT customer, COUNT(*) AS orders
FROM data
GROUP BY customer
HAVING orders > 2

Six customers, each with three orders.

Why HAVING and not WHERE

WHERE looks at one row at a time, before anything is grouped. At that point there is no "number of orders per customer" yet, only single orders. HAVING runs after GROUP BY, when each customer has become one row with a count, so that is where a condition on the count belongs.

For fast finishers

  1. How much paid revenue came in each month? (SUBSTR, GROUP BY)

  2. What is the average paid order value in each channel? (AVG, ROUND)

  3. Which product sold the most units in paid orders? (SUM, ORDER BY, LIMIT)

Solutions for tasks 7 to 9
Task 7
SELECT SUBSTR(date, 1, 7) AS month, ROUND(SUM(revenue), 2) AS revenue
FROM data
WHERE status = 'paid'
GROUP BY SUBSTR(date, 1, 7)
ORDER BY month

January 1587, February 2081, March 3228, April 2573. The playground cannot group by the alias month, so the SUBSTR expression is repeated in GROUP BY.

Task 8
SELECT channel, ROUND(AVG(revenue), 2) AS avg_order
FROM data
WHERE status = 'paid'
GROUP BY channel
ORDER BY avg_order DESC

Marketplace 412.33, Partner 405.33, Direct 351.00. Direct brings the most revenue (task 5) because it has the most orders, not the largest ones.

Task 9
SELECT product, SUM(units) AS units
FROM data
WHERE status = 'paid'
GROUP BY product
ORDER BY units DESC
LIMIT 1

Starter Plan, with 46 units. It is the cheapest product, so it brings the least revenue of the three.

Common mistakes

  • Forgetting the status filter. Without WHERE status = 'paid', refunded and pending orders are added to the revenue.

  • A missing comma between columns. SELECT customer company shows the customer column under the heading "company", without an error.

  • WHERE with a count. WHERE COUNT(*) > 2 fails; conditions on groups belong in HAVING.

  • Expecting upper and lower case to matter. In the playground country = 'germany' still finds Germany. In SQLite and most databases it would not, so it is a good habit to write values exactly as they are stored.

Differentiation

  • Easier: learners click the Try examples in the playground first and change one thing at a time. Do tasks 1, 2 and 4 only, and task 5 together.

  • Harder: tasks 7 to 9 above; then the share of each channel in paid revenue, in percent (Direct 48.2, Marketplace 26.1, Partner 25.7).

  • Pairs: one learner writes the query, the other predicts the number of rows before it runs; swap for each task.

Homework

Write three business questions about the table that are not in the tasks, and the query that answers each. Check that each query runs in the playground. The sample orders dataset has ten more tasks with checked answers if learners need ideas.

Going further

  • Use your own data: the playground loads any CSV file, for example open data from a statistics office.

  • The playground queries one table. Joining two tables, subqueries and writing data need a full database system, which makes a good next lesson. The SQL sandbox runs SQLite in the browser with linked tables, and the SQL joins lesson is a 90-minute plan for it.

Reuse this material

CC BY 4.0

You may copy, adapt and share it, also commercially, as long as you credit ManyPI and link back.

"Analyse sales data with SQL in the browser" by ManyPI (https://manypi.com/edu/sql-sales-data-exercise), licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)