×

sql · level 1 · unit 7 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Real database jobs use every piece, one after another.

Real jobs need several statements in a row. CREATE TABLE builds the table. INSERT fills it. UPDATE keeps it up to date. Then queries with WHERE, ORDER BY and aggregates answer the questions.

This bake sale log builds a table and adds three cakes. Five more scones get sold, so an UPDATE records them. The last two queries list the best sellers and add up the takings.

When an answer looks wrong, you : read the error or the result, find the bug, fix it and run again. Every database expert does this every day.

CREATE TABLE sales (id INTEGER PRIMARY KEY, cake TEXT NOT NULL, price REAL, sold INTEGER);
INSERT INTO sales (cake, price, sold) VALUES ('Brownie', 1.5, 12), ('Scone', 1.25, 8), ('Flapjack', 1.0, 14);
UPDATE sales SET sold = sold + 5 WHERE id = 2;
SELECT cake, sold FROM sales WHERE sold > 10 ORDER BY sold DESC;
SELECT SUM(price * sold) AS takings FROM sales;

Stuck? Run one statement at a time and check each result before adding the next.