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. Chain named steps
What are the category totals for North?
Later CTEs can read earlier CTEs. A CTE is a common table expression: a named query step.
Change one thing: Change North to South. Expect Games 30 and Garden 25.
2. Reuse a named step
Can one query compare region totals?
The same CTE can appear more than once with different aliases.
Change one thing: Swap the region filters. Expect 55 and 85.
3. Use a derived table
Which categories have at least 45 sales?
A query inside FROM is a derived table. Give it an alias.
Change one thing: Use sales >= 60. Only Books remains.
4. Use a scalar subquery
Which orders equal the largest amount?
This subquery supplies one value.
Change one thing: Replace MAX with MIN. Expect order 6 with amount 10.
5. Test existence
Which customers have any sales?
EXISTS checks whether the inner query returns any rows.
Change one thing: Use NOT EXISTS. Only Cy remains.
6. Filter with a subquery
Which orders belong to known customers?
IN compares against values returned by a query.
Change one thing: Limit the inner query with WHERE id = 1. Expect orders 10 and 11.