field guide❋ Page 1 of 15 palette size
SQL · Garden
a learner's first walk through the beds
❋
— Chapter —
I

First Basket

in which the gardener walks the rose bed for the first time,
picks every flower, then learns to pick only some
❋
Spring · The Rose Bed

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.

The garden gate. The rose bed is just beyond.

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.

The rose bed. One bed, five roses. The bed is a table.

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: 1 species: Old English color: pink height: 18
One flower in the bed = one row in the table.
rose_bed
idspeciescolorheight
1Old Englishpink18
2Crimson Glorypurple22
3Madame Hardypink24
4Icebergwhite16
5Carefree Beautypink20
The same bed as a table. The rows are the flowers.

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.

your basket (empty for now)
The basket starts empty. The query decides what goes in.

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.

first-basket.sql
the rose bed
SELECT * FROM rose_bed
Press Run query to see every rose hop into the basket.
Field guide: a SELECT always names which columns to keep (* 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:

first-basket.sql
the rose bed
SELECT color, height FROM rose_bed
Run the query. Every rose is still in the basket — but the basket only carries two facts per rose.
Notice: the number of rows in the basket didn't change — still all five roses. Picking fewer columns doesn't filter out flowers; it just trims down what each one tells you.

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.

first-basket.sql
the rose bed
WHERE color = 'pink'
The purple and white roses fade. Only the three pink roses light up.
Field guide: WHERE is a sieve held over the bed. Each flower drops through the condition; only the matching ones land in the basket.

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.

first-basket.sql
the rose bed
WHERE height > 16
A ruler at 16 cm. Four roses stand taller — Old English, Crimson Glory, Madame Hardy, Carefree Beauty. Only Iceberg, at 16 cm exactly, stays in the bed.
The comparisons: = 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.

first-basket.sql
the rose bed
ORDER BY height DESC
Same three pink roses — but in the basket, the tallest comes first.
Order applies to whatever's in the basket. If you filter with WHERE first, then ORDER BY arranges the survivors. You can sort by any column — words sort alphabetically.

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.

first-basket.sql
the rose bed
LIMIT 2
Only the two tallest end up in the basket. Madame Hardy at 24, Carefree Beauty at 20.
Heads up: LIMIT without an ORDER BY gives you whatever rows the database returned first — usually fine for a peek, never reliable for "the top ones."

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
which columns to keep on each rose's tag
FROM
which bed to walk
WHERE
which flowers to put in the basket
ORDER BY
how to arrange the basket
LIMIT
how many to keep
SELECT species, color, height
FROM rose_bed
WHERE color = 'pink'
ORDER BY height DESC
LIMIT 2;
All five clauses, in the only order they're allowed.

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.

scratch.sql
the rose bed
your query
Edit the query and run it. The bed answers.
⌘/Ctrl + Enter to run
Result —
Run a query to see rows here.

rose_bed — schema

idinteger · primary key
speciestext · the rose's cultivar name
colortext · 'pink', 'purple', or 'white'
heightinteger · centimeters off the ground
Suggestions to try: 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.

SELECT *
picked every rose with every trait
SELECT cols
picked every rose but only some traits
WHERE
filtered which roses ended up in the basket
ORDER BY
arranged the basket before bringing it home
LIMIT
kept only the first few

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.

SELECT species, color FROM rose_bed WHERE color = 'pink';

2. Pick the species and height of every pink rose taller than 18 cm.

SELECT species, height FROM rose_bed WHERE color = 'pink' AND height > 18;

3. Find the two tallest roses. Show their species and height, tallest first.

SELECT species, height FROM rose_bed ORDER BY height DESC LIMIT 2;

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.

Five roses. One basket. End of day one.
· Sunset ·
❋
end of Chapter
I
the basket comes home
the gate closes for the day
❋
an intermission is recommended · Chapter II awaits