Foundations practice set

Lesson 10 of 10 · 22 min0/6 checked in practice
Objective

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.

  1. Which table? Find where the facts live. Use the schema panel or information_schema.columns if you're unsure.
  2. What is one row? Know the grain of the table and of the result you're asked for.
  3. Which columns, called what? Use the exact names in the request; alias every calculation.
  4. Which rows? Filter with WHERE if the request names a subset (one customer, one category).
  5. What order, how many? Sort only when asked, add a tie-breaker, and LIMIT last.
  6. 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:

ExampleWarm-up: cheapest Electronics product
-- 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:

ExampleWarm-up: the lines of one Wadi Market order
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;
Wadi Market online store

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.

Now practise
6 exercises on real data, checked in your browser (practice).