SQL
Most candidates can write SELECT ... FROM ... WHERE. Far fewer can explain why a query with an index took 40 ms and the same query without one took four seconds, or what happens to a row between two concurrent transactions that both read and write it. Those are the questions that separate people who use a database from people who operate one.
This section assumes you can already write a basic query and focuses on the three areas where interviews go deep: how tables are combined, how the planner finds rows, and what isolation actually guarantees.
The 60-second answer
Section titled “The 60-second answer”“A relational database stores rows in pages, and every query is executed by a planner that chooses between scanning a table and probing an index. Joins are the algebra for combining tables: inner joins keep matches, outer joins keep unmatched rows padded with NULL, and the ON clause decides what counts as a match while WHERE filters the result afterwards. Indexes are ordered structures — usually B-trees — that turn a scan into a few page reads; a composite index only helps if you constrain its leading columns. Transactions group statements into one atomic unit, and the isolation level decides which concurrency anomalies you can still see: dirty reads, non-repeatable reads, phantoms and lost updates. The default in PostgreSQL is READ COMMITTED, which prevents dirty reads and nothing else.”
The mental model
Section titled “The mental model”table → heap of pages, each holding rowsindex → separate ordered structure: key → row pointer (ctid)query → parser → planner (cost model) → executorplan → Seq Scan | Index Scan | Index Only Scan | Bitmap Heap Scan | Nested Loop | Hash Join | Merge JoinThree facts follow, and they explain almost every performance question:
- An index does not make a query fast by existing. It helps only if the predicate can be answered from the index’s leading columns.
- The planner is cost-based. It chooses a sequential scan over an index when the predicate matches a large fraction of the table, because random page reads are expensive.
NULLis not a value. It is unknown:NULL = NULLisNULL,COUNT(col)skips it, and an outer join produces it.
How this section is organised
Section titled “How this section is organised”- SQL Joins — inner, left, right, full and cross joins, self joins, anti-joins, and how to choose between a join and a subquery.
- Indexes — B-tree structure, composite
indexes and column order, covering indexes, selectivity,
EXPLAIN, and when an index hurts. - Transactions — ACID, isolation levels and the anomalies each permits, row and table locks, deadlocks, optimistic vs pessimistic locking.
If you are preparing for a Rails role, read the Active Record page alongside these: the ORM generates exactly this SQL, and the N+1 and indexing problems are database problems wearing Ruby clothes.
A schema to reason with
Section titled “A schema to reason with”Every example in this section uses the same three tables.
CREATE TABLE authors ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text NOT NULL, email text NOT NULL UNIQUE);
CREATE TABLE books ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, author_id bigint NOT NULL REFERENCES authors (id), title text NOT NULL, price numeric(10, 2) NOT NULL CHECK (price >= 0), published_on date);
CREATE TABLE reviews ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, book_id bigint NOT NULL REFERENCES books (id), rating smallint NOT NULL CHECK (rating BETWEEN 1 AND 5), body text);Small enough to hold in your head, rich enough that every join shape has a meaningful example: authors write books, books receive reviews, and some authors or books have none — which is where outer joins earn their keep.
Prerequisites
Section titled “Prerequisites”SELECT,WHERE,GROUP BY,HAVING,ORDER BY.- Primary keys, foreign keys,
NULLsemantics. - The difference between a logical operation and its physical implementation.
If GROUP BY and NULL are shaky, fix those first — every advanced question in this section assumes them.
Further reading
Section titled “Further reading”- PostgreSQL: The SQL Language
- Use The Index, Luke — indexing, explained properly.
- PostgreSQL: Using EXPLAIN