By the end of this lesson you can answer simple business requests on an unfamiliar database without guidance: find the right table, check its structure, and return correctly named, correctly shaped results.
Why it matters: At work, nobody tells you which lesson a request belongs to. This set mixes the skills of the track the way real requests do, so you practise choosing the tool as well as using it.
Dataset: Starter shop. Examples run in the editor on the right.
This lesson is a workout rather than new material. Each exercise reads like a request from a colleague. Before writing anything, run through the checklist below; it's the habit that separates careful analysts from fast-but-wrong ones.
- Which table? Find where the facts live. Use the schema panel or
information_schema.columnsif you're unsure. - What is one row? Know the grain of the table and of the result you're asked for.
- Which columns, called what? Use the exact names in the request; alias every calculation.
- Which rows? Filter with WHERE if the request names a subset (one customer, one category).
- What order, how many? Sort only when asked, add a tie-breaker, and LIMIT last.
- Check the result. Does the row count make sense? Are there NULLs, and are they real?
The first exercises use Corner Market (starter); the later ones use Wadi Market (shop), which you explored in lesson 8. The task says which. Here's a warm-up that uses four of the six checklist steps at once:
-- table: products; one row per product
SELECT product_name,
price
FROM products
WHERE category = 'Electronics'
ORDER BY price, product_name
LIMIT 1;WHERE keeps the 2 Electronics products, ORDER BY sorts them cheapest first (with the name as a tie-breaker), and LIMIT 1 keeps the cheapest: the Wireless Mouse.
And a Wadi Market reminder: in order_items, a line's value is its quantity times its unit price, minus the line's discount. That's a calculated column, so it needs an alias:
SELECT line_no,
product_id,
quantity,
unit_price,
discount_amount,
quantity * unit_price - discount_amount AS line_total
FROM order_items
WHERE order_id = 100002
ORDER BY line_no;Order 100002 has three lines, each with a discount. Multiplication happens before subtraction (as in ordinary arithmetic), so each line_total is quantity × unit price, then minus that line's discount.