First Basket
picks every flower, then learns to pick only some
Page 2 · Welcome
A garden, but for SQL
tables are beds, rows are flowers, queries are the gardener's walk
You're standing at the gate of a garden. Inside are flower beds, paths between them, a greenhouse, a directory at the gate, and an open table where you sort what you've gathered. You are the gardener.
Over the rest of this chapter you'll learn the first verb of SQL — SELECT — by walking one of the beds, the rose bed, with a basket. You'll pick every rose. You'll pick only the pink ones. You'll arrange your basket tallest-to-shortest. You'll take only the first two. That's the lesson. By the end, you'll have written real SQL queries against a real database living inside this page.
There is nothing to install. The database is running already, waiting in your browser. The roses are planted. Let's open the gate.
A note about the keys: you can press → to go to the next page, ← to go back, or click Next at the bottom. The breadcrumb above ("First Basket") opens the full outline of the lesson.
Page 3 · The bed
Tables are beds
a bed of roses, every rose a row
Every SQL database is a garden. Inside it are tables —
these are the flower beds. Each bed holds one kind of thing. There's a bed of roses,
a bed of tulips, a bed of daffodils. Today we are tending one bed only: the rose_bed.
Five roses are planted in it. Some are pink, one is purple, one is white. They aren't all the same height — Madame Hardy is tall and grand; Iceberg is the shortest at 16 cm.
If you came back tomorrow and planted a sixth rose, the bed would have six roses. The bed itself doesn't change — only how many flowers are in it. That's what a table is: the named container, the shape of what it holds, the soil it sits in. The flowers come and go; the bed remains.
Page 4 · Flowers and tags
Rows are flowers,
columns are traits
each rose carries a tag with its name, color, and height
Crouch down next to one of the roses. Tied around its stem is a small wooden tag, and stamped onto the tag are four things: its id number (every rose has a unique one), its species, its color, and its height in centimeters.
Every rose in the bed has a tag with these same four fields filled in. The fields are columns. Each individual rose is a row.
| id | species | color | height |
|---|---|---|---|
| 1 | Old English | pink | 18 |
| 2 | Crimson Glory | purple | 22 |
| 3 | Madame Hardy | pink | 24 |
| 4 | Iceberg | white | 16 |
| 5 | Carefree Beauty | pink | 20 |
The bed on the left and the table on the right are the same data, shown two ways. As a gardener you'll mostly walk the bed. As a SQL speaker you'll mostly look at the table. Every time you write a query, picture the bed.
Page 5 · The basket
The basket
SELECT is the gardener walking the bed with a basket
You pick up a wicker basket from beside the gate. You're going to walk down the row of roses and, for each one, decide whether to put it in your basket. When you reach the end of the bed, you turn around and look at what you've gathered.
That walk is what a SELECT statement is. The basket is your result. Some queries fill the basket with everything. Some leave most of the bed untouched. Some sort what you've picked before handing it to you. Same walk, different baskets.
On the next page you'll write your first query — and that empty basket will fill up.
Page 6 · SELECT *
Your first basket
pick everything in the bed
The simplest walk: stop at every rose, and put every one of them in the basket.
Two words tell SQL to do this: SELECT * ("pick everything"),
and FROM rose_bed ("from this bed"). The * is
shorthand for "all columns" — keep every trait on every flower's tag.
Press Run query below. Watch the basket fill.
* means all of them)
and which table to read FROM. The semicolon at the end is a quiet
signal that the sentence is done.
Page 7 · SELECT specific columns
Don't read every tag
pick only the columns you care about
You don't have to read every word on every rose's tag. If you only need the color and the height,
tell SQL just that. Replace the * with the column names, separated by commas:
Page 8 · WHERE on text
Only the pink ones
filter rows with WHERE
Now you want only the pink roses. The purple one stays in the bed. The white one stays in the bed. To tell SQL "skip any flower that isn't pink," you add a WHERE clause.
The condition reads like an English sentence: color = 'pink'.
Two things to notice: a single = (not two), and the word
'pink' wrapped in single quotes because it's text.
Page 9 · WHERE on numbers
Only the tall ones
numbers let you ask for "more than" and "less than"
For words you usually ask "is it exactly this?" For numbers you can ask a whole family of questions:
- more than
height > 16 - at least
height >= 18 - less than
height < 20 - at most
height <= 18 - not equal
height != 18(or<>, same thing) - in a range
height >= 18 AND height <= 22
That last one introduces AND, which glues two conditions together — a flower must satisfy both to land in the basket. (SQL also has OR for either condition, and NOT to flip a condition; the sandbox at the end of the chapter will let you try them.)
Let's find the roses taller than 16 cm. No quotes around the number — it's a number, not text. Try the chips below to see each operator pick a different set of roses.
= equals,
!= or <> not equal,
< less than,
> greater than,
<= at most,
>= at least.
Page 10 · ORDER BY
Tallest first
arrange the basket before handing it over
The roses grew in the bed in whatever order they were planted — that's not necessarily the order you want them in your basket. ORDER BY lets you arrange what you've picked.
Let's keep the three pink roses but lay them out tallest-first. DESC
means "descending" — biggest down to smallest. Use ASC (or just leave it off)
for smallest first.
Page 11 · LIMIT
Just two please
take only the first few
What are the two tallest roses in the whole bed? Sort by height (tallest first), then take only the first two.
LIMIT is the wicker lid you put over the basket once it has enough. It always comes last in the query.
Page 12 · Putting it together
The anatomy of a query
five clauses, always in this order
You've now met every clause used in this lesson. Their order in the sentence is fixed — you can leave parts out, but you can't shuffle them.
SELECT species, color, height FROM rose_bed WHERE color = 'pink' ORDER BY height DESC LIMIT 2;
SQL reads in roughly that same order in your head: walk this bed, keep these flowers, arrange them like so, take this many, and show me these traits. The strange part is that SELECT is written first but conceptually applies last — by the time it runs, the bed has already been walked and the basket is full. SQL is asking you to declare what you want, not to spell out the steps.
Page 13 · Your own basket
Walk the bed yourself
write anything; the database will answer
The query box below is yours. Type any SELECT, ORDER BY, WHERE you like — the rose bed table is still seeded with the same five roses. Press Run query when ready. Below the scene you'll see the result as a proper table.
rose_bed — schema
SELECT species, height FROM rose_bed WHERE color = 'pink' OR color = 'purple';
·
SELECT * FROM rose_bed WHERE NOT color = 'pink';
·
SELECT color, height FROM rose_bed ORDER BY height DESC LIMIT 3;
Page 14 · End of Chapter I
The basket comes home
what you've learned, and what's next
You walked the rose bed five different ways. Each time, the basket came back filled differently. Without realising it, you wrote real SQL against a real SQLite database that lives entirely inside this browser tab.
Check your basket
Three questions to confirm you've got the hang of it. Write a query that answers each one. Press Check and the basket will tell you if it matches.
1. Pick just the species and color of every pink rose.
2. Pick the species and height of every pink rose taller than 18 cm.
3. Find the two tallest roses. Show their species and height, tallest first.
The rest of Spring contains two further gardens. The first is called Tending the Bed:
here the gardener turns to the soil itself — planting new flowers, altering the ones already there,
removing those no longer wanted. The actions are named INSERT,
UPDATE, and DELETE. The second is called
the Greenhouse: in it no flower is yet planted — only a card is drafted, on which the gardener
writes the columns of the future bed and stamps the rules it will obey. From this card, when the
time comes, an actual bed will be dug.
After Tending the Bed, after the Greenhouse, two more gardens wait in Summer. One is called the Flight Path: it is travelled by bees, between the hive and the open flower, and it is never the same path twice. The other is called the Sorting Table: here the day's basket is set down, and its contents are placed into smaller baskets, one color to each.
But for today, the basket has come home, the gate has been closed, and the path between the beds is empty. The day's work is done. The walk has been a good one.
the gate closes for the day