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 columnsorder_id,date,customer,company,country,channel,product,units,unit_price,revenueandstatus.
Running the lesson
45 min- 5 min
Everyone opens the playground, loads the sample and runs SELECT * FROM data LIMIT 10. One row is one order.
- 25 min
Learners work through the six tasks below in pairs. Each task names the SQL it practises.
- 10 min
Compare queries. Different queries can give the same right answer, which is worth pointing out.
- 5 min
Exit question: why does task 6 need HAVING and not WHERE?
Tasks
Which orders came from Germany? Show the order number, customer, company and revenue. (WHERE)
List every order with more than 400 in revenue, the largest first. (WHERE, ORDER BY)
Which companies have "trading" or "supply" in their name? Each company once. (DISTINCT, LIKE)
How many orders are paid, refunded and pending? (GROUP BY, COUNT)
How much paid revenue did each sales channel bring in? (WHERE, GROUP BY, SUM)
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
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
SELECT order_id, company, revenue
FROM data
WHERE revenue > 400
ORDER BY revenue DESC8 orders, from 798.00 (orders 1016 and 1026) down to 447.00.
3. Trading and supply companies
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
SELECT status, COUNT(*) AS orders
FROM data
GROUP BY status25 paid, 3 refunded and 2 pending.
5. Paid revenue by channel
SELECT channel, ROUND(SUM(revenue), 2) AS revenue
FROM data
WHERE status = 'paid'
GROUP BY channel
ORDER BY revenue DESCDirect 4563, Marketplace 2474 and Partner 2432.
6. Customers with more than two orders
SELECT customer, COUNT(*) AS orders
FROM data
GROUP BY customer
HAVING orders > 2Six 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
How much paid revenue came in each month? (SUBSTR, GROUP BY)
What is the average paid order value in each channel? (AVG, ROUND)
Which product sold the most units in paid orders? (SUM, ORDER BY, LIMIT)
Solutions for tasks 7 to 9
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 monthJanuary 1587, February 2081, March 3228, April 2573. The playground cannot group by the alias month, so the SUBSTR expression is repeated in GROUP BY.
SELECT channel, ROUND(AVG(revenue), 2) AS avg_order
FROM data
WHERE status = 'paid'
GROUP BY channel
ORDER BY avg_order DESCMarketplace 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.
SELECT product, SUM(units) AS units
FROM data
WHERE status = 'paid'
GROUP BY product
ORDER BY units DESC
LIMIT 1Starter 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 companyshows the customer column under the heading "company", without an error.WHERE with a count.
WHERE COUNT(*) > 2fails; 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.
