Starting PostgreSQL in your browser… (first time ~3 MB, then cached)
Lesson 01🔎Querying basicsFoundation
SELECT: ask for the columns you need
A query names the columns it wants and the table they live in. PostgreSQL hands back a new table: the result.
You’ll useSELECT · FROM · column lists · * · AS · expressions
Watch the query work
Every query you will write starts here. `SELECT` says **which columns** you want, `FROM` says **which table** they come from, and the answer is always a fresh table of rows. We'll ask the café's `products` table a few questions.
The trace starts as soon as PostgreSQL is ready…
The mental modelA query never changes the table it reads. It builds a **new result table**: one output row per input row (until you filter), with exactly the columns you list, in the order you list them.
Why it mattersReal tables have dozens of columns. Naming only the ones you need keeps results readable, keeps reports stable when someone adds a column, and moves less data across the network.
🐘 Open the 🗂 Tables panel any time to see every table and column of the café database.
The moves, line by line
Tap a note to see its line.
⌘/Ctrl + Enter · each run starts from a fresh database
Run to see each result
line 2`*` expands to every column, in the order the table defines them.
line 5Columns are separated by commas. No comma after the last one.
line 6`FROM` names the table. A query can span several lines; the `;` ends it.
line 11`price - cost` is computed for each row. `AS margin` names the result column.
line 12Maths with a decimal: `numeric` keeps exact pennies, so `2.40 * 1.2` is `2.880`.
line 13`||` joins text. Single quotes make a text value.
Gotchas
A trailing comma before `FROM` is a syntax error: `SELECT name, price, FROM products`.
`'name'` in single quotes is the word *name*, not the column. Use double quotes, or none, for column names.
Unquoted names are lower-cased: `SELECT Name` finds `name`, but `AS Margin` produces a column called `margin`.
Pro tips
Lay a query out one clause per line (`SELECT`, then `FROM`). It is easier to read and to edit.
Name every calculated column with `AS`. A result column called `?column?` is no use to anyone.
Before you run it
Your turn — write the query
Find the bug
Run it, spot the trap, repair it.
Answer everything, then submit
8 of 9 correct passes the lesson and unlocks the next one.