SQL Glossary: From ACID to Window Functions
Documentation is dense with terms that are easy to nod along to and hard to explain in a meeting. This lesson defines the ones that actually change how you write queries.
- Set and relational vocabulary â grain, cardinality, selectivity, relation
- Planner and performance terms â sargable, plan, statistics, cost model
- Transaction and schema terms â ACID, isolation level, DDL, materialized view
- Terms demonstrated, not just defined â every cluster of words gets a query you can run Vocabulary is the cheapest form of expertise you can buy.
Prerequisites
- !Basic familiarity with SELECT and JOIN
- !Willingness to skim a reference and return as needed â this page is meant to be revisited
What You'll Learn
- 1Define the core relational terms precisely enough to use them correctly in review
- 2Explain planner vocabulary such as selectivity, sargability, and cost
- 3Distinguish transaction terms like isolation level and snapshot
- 4Recognize which terms signal performance or correctness risk in production discussions
The Language Around Sets and Relations
SQL operates on relations â tables in the everyday sense, though formally a relation is an unordered set of tuples with no duplicate rows. Your result of a SELECT is a result set, another relation. Because sets are unordered, any guarantee about row order comes from an explicit ORDER BY, never from insertion order or physical storage.
Grain describes what one row of a table means: one row per customer, one row per customer-order line, one row per event. Almost every metric bug in analytics traces back to a grain mistake â joining a daily-aggregate table to an event-level table and silently multiplying rows. Cardinality is the count of rows, or in the case of a join, the number of distinct key combinations produced. Fan-out is what happens when a one-to-many join multiplies your measure columns; catching fan-out early is a cardinality check.
Tuple is a single row. Domain is the permitted set of values for a column â integers from 1 to 100, or a controlled vocabulary of status codes. Enforcing domains through CHECK constraints keeps garbage out of analyses before it becomes someone else's mystery.
| Term | Short definition | Why you care |
|---|---|---|
| Relation | A set of rows with unique column names, unordered | Explains why row order needs ORDER BY |
| Tuple | One row of a relation | Unit of locking and versioning in MVCC systems |
| Grain | What one row represents | Determines whether a join is safe |
| Cardinality | Number of rows or distinct key combinations | Drives join strategy and index choices |
| Fan-out | Row multiplication from a one-to-many join | Produces inflated sums and counts silently |
| Domain | Allowed values of a column | Enforced with CHECK constraints |
| Candidate key | Minimal set of columns that uniquely identifies a row | Basis for primary key selection |
Planner and Performance Vocabulary
The planner's vocabulary appears every time you read an execution plan or argue about an index.
Statistics are sampled summaries the planner keeps per table and column: row counts, histograms of value distributions, and correlations between columns. Stale statistics lead to wrong plans, which is why bulk loads are followed by ANALYZE.
Selectivity is the fraction of rows a predicate is expected to keep. An equality filter on a unique key has selectivity near one-over-n; a WHERE status = 'active' on a table that is 80 percent active has selectivity near 0.8 and often cannot benefit from an index.
Cost model is the planner's internal pricing scheme. Costs combine estimated page reads, CPU per row, and setup per operator. They are relative units, not seconds â compare them only within one plan.
Sargable (search-argument able) describes a predicate the planner can evaluate using an index: comparing an indexed column directly to a constant or range. Wrapping the column in a function â WHERE LOWER(email) = ... or WHERE YEAR(order_date) = 2024 â usually defeats a plain index, making the predicate non-sargable. Modern fixes include expression indexes and rewriting the predicate to isolate the column: WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01'.
A plan is the operator tree the executor runs. An index scan walks an index to find matching rows; a sequential scan reads the table from start to end. Blocking versus non-blocking operators distinguishes sorts and hash builds (which must consume their whole input) from streaming operators that emit rows as they arrive.
Transactions, Locks, and Isolation
ACID summarizes transaction guarantees: Atomicity (all-or-nothing), Consistency (constraints hold at commit), Isolation (concurrent transactions do not observe each other's intermediate state), and Durability (committed data survives crashes).
Isolation level chooses which anomalies are permitted. Read Uncommitted allows dirty reads; Read Committed prevents them but permits non-repeatable reads; Repeatable Read prevents non-repeatable reads but may permit phantoms in some engines; Serializable prevents all three at the cost of concurrency. PostgreSQL's implementation of Read Committed uses snapshot isolation per statement, while Repeatable Read uses a single snapshot for the whole transaction â a difference you feel during long analytical transactions.
A deadlock occurs when two transactions wait on each other's locks; engines detect and abort one. A lock upgrade happens when a transaction holds a shared lock and requests an exclusive one, a common deadlock source in read-then-update flows. MVCC (multi-version concurrency control) keeps old row versions so readers never block writers; the trade-off is vacuuming or purging of obsolete versions, which is what PostgreSQL's autovacuum and InnoDB's purge threads handle.
Schema and Object Vocabulary
DDL (data definition language) creates, alters, and drops objects â CREATE, ALTER, DROP. DML (data manipulation language) touches rows â INSERT, UPDATE, DELETE, MERGE. DCL (data control language) manages permissions â GRANT, REVOKE. TCL (transaction control) â COMMIT, ROLLBACK, SAVEPOINT.
A view is a stored query text, expanded at compile time; a materialized view stores the result and must be refreshed. A CTE is a named subquery valid only for one statement. A temp table lives for a session or transaction and can be indexed. A sequence generates ordered numbers, often backing primary keys. A constraint is a declared rule â PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK â enforced by the engine, not by convention.
Normalization is the practice of decomposing data to remove redundancy, commonly to third normal form. Denormalization deliberately reintroduces copies for read speed, common in reporting marts. Both are tools; neither is a morality.
-- A tiny schema that names many of these terms at once
CREATE TABLE departments (
department_id BIGSERIAL PRIMARY KEY, -- surrogate key backed by a sequence
name TEXT NOT NULL UNIQUE,
budget NUMERIC(12, 2) CHECK (budget >= 0) -- domain constraint
);
CREATE TABLE employees (
employee_id BIGSERIAL PRIMARY KEY,
full_name TEXT NOT NULL,
department_id BIGINT NOT NULL REFERENCES departments (department_id),
hired_on DATE NOT NULL,
salary NUMERIC(10, 2) NOT NULL CHECK (salary > 0)
);
-- Index on a sargable predicate column; correlation stats help the planner
CREATE INDEX idx_employees_dept ON employees (department_id);
-- A view (logical) versus a materialized view (stored)
CREATE VIEW dept_headcount AS
SELECT
d.department_id,
d.name,
COUNT(e.employee_id) AS headcount,
ROUND(AVG(e.salary), 2) AS avg_salary
FROM departments d
LEFT JOIN employees e ON e.department_id = d.department_id
GROUP BY d.department_id, d.name;
CREATE MATERIALIZED VIEW dept_headcount_mv AS
SELECT
d.department_id,
d.name,
COUNT(e.employee_id) AS headcount,
ROUND(AVG(e.salary), 2) AS avg_salary
FROM departments d
LEFT JOIN employees e ON e.department_id = d.department_id
GROUP BY d.department_id, d.name;
Terms You Will Meet in Query Plans
Reading plans introduces a few more words worth pinning down early. An estimate is the planner's predicted row count; the actual is what the executor observed. Their ratio is the first thing performance engineers check. Startup cost versus total cost separates what an operator spends before emitting its first row from the full run â relevant when LIMIT lets the executor stop early. Loops tells you an inner node executed once per outer row; multiply its time by loops before judging its importance. Shared buffers (PostgreSQL) or the buffer pool (InnoDB) is the engine's cache of data pages, and temp files in a plan indicate sorts or hash joins that spilled to disk because they exceeded the memory budget.
-- Selectivity and sargability in action: same intent, different plans
-- Non-sargable: function on the column defeats a plain index on hired_on
EXPLAIN SELECT employee_id FROM employees WHERE EXTRACT(YEAR FROM hired_on) = 2023;
-- Sargable rewrite: range predicate the planner can match to an index
EXPLAIN SELECT employee_id FROM employees
WHERE hired_on >= DATE '2023-01-01' AND hired_on < DATE '2024-01-01';
-- Cardinality check before aggregating a join: does the join fan out?
SELECT
COUNT(*) AS joined_rows,
COUNT(DISTINCT e.employee_id) AS distinct_employees
FROM employees e
INNER JOIN departments d ON d.department_id = e.department_id;
-- If joined_rows exceeds distinct_employees, the join multiplied rows.
How This Vocabulary Shows Up at Work
You rarely deliver a glossary recital; you use these words to make decisions. Saying "that predicate is not sargable, so the index will not fire" is faster and more precise than "the query feels slow." Saying "our grain on this table is one row per customer per day" prevents a class of join bugs before they ship. Saying "we are on Repeatable Read, so the snapshot is from transaction start" explains why a long-running report did not see this morning's inserts.
When you hit an unknown term in documentation, resist skimming past it. Look it up, write one sentence in your own words, and â if possible â construct a five-line query that demonstrates it. Terms you have executed once stick far longer than terms you have only read.
Common Mistakes
Assuming tables return rows in insertion order without ORDER BY
Fix: Add ORDER BY whenever order matters; treat unordered results as sets
Blaming the index when a predicate wraps the column in a function
Fix: Rewrite the predicate sargably or create an expression index on the function
Confusing grain with primary key
Fix: The primary key identifies rows; the grain is what each row means in the business model â write it down per table
Quoting cost numbers as if they were milliseconds
Fix: Costs are planner-internal units; use EXPLAIN ANALYZE actual times for wall-clock figures
Best Practices
- Document grain and domain rules next to each table definition
- Refresh statistics after bulk loads so planner vocabulary reflects reality
- Learn your engine's isolation level semantics, not just the SQL standard names
- When a new term appears in a plan or vendor doc, demo it with a small query before moving on
Key Takeaways
- Relations are unordered; row order is guaranteed only by ORDER BY
- Grain mistakes cause silent metric inflation â always know what one row means
- Sargability determines whether your index can be used at all
- ACID and isolation levels describe which concurrency anomalies your transaction tolerates
- Views store logic; materialized views store results and need refresh strategy