Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

alias
A short second name given to a table or column in a query, so the rest of the query can refer to it briefly.
Cartesian product
Every row of one table paired with every row of another, which happens when a join has no matching rule.
column
One named field in a table, with the same kind of value in every row, such as a title or a year.
constraint
A rule a table enforces on its data, such as requiring a value or making one column unique.
distinct
A word in a query that removes repeated rows from the result, so each one appears once.
foreign key
A column that refers to the primary key of another table, so each row can be tied to one row there.
index
A stored lookup that helps the database find rows quickly without reading the whole table.
inner join
The default way of combining two tables: only pairs of rows present on both sides appear in the result.
join
A query operation that combines rows from two tables by matching values they share, usually a shared identifier.
junction table
A bridge between two tables that lets a row on either side pair with many on the other; each of its rows holds one pair.
left join
A combination that keeps every row of the first table named, filling in nulls where the second has no match.
many-to-many
A relationship where one row on each side can pair with many rows on the other, usually held in a junction table.
null
A marker for a value that is missing or unknown, which is not the same as zero or an empty string.
outer join
A join that keeps rows from one or both tables even when no row on the other side matches.
primary key
The column or columns that together identify each row uniquely, and which can never be missing.
query
A request written in SQL that asks the database for an answer, usually a table of rows.
row
One record in a table, with one value for each column that table defines.
schema
The stored plan of a database: its tables, the columns in each, and the rules those columns must follow.
table
A named set of rows that all share the same columns, such as one holding every book in a library.

SQL from the ground up

How SQL works

A database is tables of rows and columns, and SQL is how you ask them questions — one sentence that returns a table of answers.

Part 1 of 12

Beginner13 min readBack to Courses

Tables, rows, columns

A relational database stores data in tables, and a table is exactly what it sounds like: a fixed set of columns, and any number of rows filling them in. A row in a books table has a title, an author, a year — one book per row, the same shape for every row.

The shape is fixed on purpose. Because every row has the same columns, the database can answer questions about them quickly and honestly: which books came out after 1960, how many authors there are, what the average year is.

A question, written as a sentence

SQL is how you ask. A query says what you want, not how to find it: SELECT title FROM books; asks for one column of one table, and the answer comes back as a table of its own — the rows that match, with only the columns you asked for.

Reading a query aloud is usually enough to understand it. SELECT names the columns, FROM names the table, and the clauses after them narrow the rows: WHERE keeps some, ORDER BY sorts them, LIMIT stops early.

What the database does with the sentence

A query is not a set of instructions the way a loop is. You describe the answer — these columns, from this table, with this condition — and the database decides how to get it: which rows to read, in what order, and whether an index can skip most of the work. That is what declarative means.

The database parses the sentence into a plan, checks that every table and column exists, and only then runs it. That is why a misspelled column fails before any row is touched: the error is about the sentence, not the data.

The plan is not part of the query and may change as the data grows. The same SQL can be slow on a table with no index and fast once one exists, without a single character of the query changing.

A statement, and the table it answers with

SQL has statements that ask (SELECT) and statements that change (INSERT, UPDATE, DELETE, CREATE). This course starts with asking, because a query cannot damage anything: the worst it can do is return an answer you did not want.

Every SELECT answers with a table — rows and columns, even when there is one row and one column. That is why results can be nested inside other queries later: the answer to one question has the same shape as the table the question was asked about.

A statement ends with a semicolon. In a file with several statements, the semicolon is what separates them; leave it out and the next statement becomes part of the same broken sentence.

Rows have no natural order

A table is a set of rows, not a list. The database stores them however it finds convenient — in the order they were written, in the order an index happens to hold them, or in whatever order a query plan produces — and none of that is part of the data.

Two runs of the same query may answer in different orders, and a new version of the database can change the order. If the order matters, the query has to say so with ORDER BY; any order you notice otherwise is an accident.

What else the file holds

A database file is more than rows. It holds the table definitions, the constraints on them, the indexes built over them, and a catalogue that records all of it — the schema. CREATE TABLE writes to that catalogue as much as to the data, which is why a second connection opened on the same file sees the same tables: the definition is stored, not remembered by the program that made it.

It is also why a mistake at this level lasts. Every query written afterwards is shaped by the columns and rules declared here, so a table that forgot to say a column is required leaves every later reader guessing whether a blank means zero, unknown or not applicable.

The smallest useful query

-- One column, every row.
SELECT title FROM books;

-- Every column. The asterisk means "all of them".
SELECT * FROM books;

Three first-query mistakes: a missing semicolon that lets two statements run together, double quotes around text (standard SQL reads those as a column name — values take single quotes), and expecting a result to be saved. A SELECT answers now; it does not create a table.

Try these next

  • Change the first example to return only the author column.
  • Add a WHERE year > 1970 and notice that the answer is smaller but still a table.
  • Write SELECT 1 + 1 AS answer; — a query with no table at all, which proves the result is just a table.

Your first query

Run the query as it is, then change it to return every column of the books table.

Live Preview

One table, three answers

Run the three queries and compare their shapes. Then add a fourth that returns only the title and year of each album.

Live Preview

Every Run builds this database from setup.sql and then runs query.sql — so the tables are the same on every run, and you can change a query as often as you like without leaving a mess behind.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What is a table, in a relational database?