Before you start
You need Workbench and a running SQL engine. Every example includes its own input rows. No catalog, files, credentials, or external data is needed. These queries create no stored tables. Copy the SQL into Workbench and run the statements in order. Compare the result with the table below the query.WITH names input data for one statement only. Each example can run on its own.
1. Calculate totals
How many orders and how much sales?
Aggregates combine input rows into a summary.
Change one thing: Add WHERE region = ‘North’. Expect 4 orders and sales of 85.
2. Compare categories
What are sales by category?
GROUP BY defines the groups to summarize.
Change one thing: Use region instead of category in SELECT, GROUP BY, and ORDER BY. Expect North 85 and South 55.
3. Use two dimensions
What are sales by category and region?
Each distinct pair forms a group.
Change one thing: Add COUNT(*) AS orders. Books/North has 2; all other groups have 1.
4. Filter groups
Which categories exceed 40 in sales?
HAVING filters after grouping.
Change one thing: Change 40 to 50. Only Books remains.
5. Count conditionally
How many large orders are in each region?
CASE can choose the values passed to an aggregate.
Change one thing: Use amount >= 25. South has 2 and North has 1.
6. Count distinct categories
How many categories does each region contain?
A distinct count removes repeated values within each group.
Change one thing: Use COUNT(*) instead. North has 4 orders and South has 2.