The Relational Model
Tables, rows, columns, keys, the shape of relational data
Tables, Rows, Columns
Spot when a list of values should become a table, name the type of each column, and predict why row order and duplicates aren't guaranteed.
Types & NULL
Pick a type for each column (text, integer, timestamp, boolean), predict what NULL does in comparisons, and decide when a column should allow NULL.
Primary & Foreign Keys
Choose between a surrogate and a natural primary key, draw the foreign-key arrow from child to parent, and pick what happens to children when the parent row is deleted.
Normalization
Split a wide spreadsheet into 1NF/2NF/3NF tables by tracing three named bugs to the duplication that causes them, and decide when denormalizing back is a measured trade.
Querying One Table
SELECT, WHERE, ORDER BY, expressions, the four moves on a single table
SELECT & Projection
Pick the columns a query returns, compute new ones with per-row expressions named by AS, and trace why the engine runs FROM before the SELECT list you wrote first.
WHERE & Filters
Filter rows with AND, OR, IN, BETWEEN, and LIKE, predict why WHERE col = NULL returns zero rows, and reach for IS NULL to match the blank.
ORDER BY, LIMIT, DISTINCT
Sort rows with single and multi-column ORDER BY, page a sorted result with LIMIT and OFFSET, and fold duplicate rows with DISTINCT, which counts two NULLs as equal.
CASE, COALESCE, Casts
Reshape each row's values with CASE branches, fill blanks with COALESCE and flip a chosen value to NULL with NULLIF, then cast a value to the type an operator demands.
Joining Tables
INNER, LEFT, RIGHT, FULL, set operations, combining rows across tables
INNER JOIN
Pair rows from two tables on a join condition, predict the result row count when a row matches once or many times (fan-out), and pick between ON and USING.
LEFT, RIGHT & FULL JOIN
Keep the unmatched rows an inner join drops with LEFT, RIGHT, and FULL, read a NULL in the result as 'no match found' rather than missing data, and isolate rows with no partner by filtering the padded key with IS NULL.
Self & Cross Joins
Alias a table into two copies so it can pair with itself for hierarchies and row comparisons, and drop the join condition with CROSS JOIN to generate every combination on purpose.
UNION, INTERSECT, EXCEPT
Stack two query results with UNION, INTERSECT, and EXCEPT, drop the duplicate-removal sort with UNION ALL when rows cannot overlap, and line the columns up by position before a reordered select list files the wrong data under the right name.
Aggregation & Grouping
COUNT, SUM, GROUP BY, HAVING, collapsing many rows to one
Aggregate Functions
Collapse a whole column to one number with COUNT, SUM, AVG, MIN and MAX, predict how each treats NULLs, and read several aggregates from one SELECT as a single summary row.
GROUP BY
Split rows into one group per distinct value, predict how grouping changes the result row count, and apply the rule that every selected column must be grouped or wrapped in an aggregate.
HAVING vs WHERE
Filter rows with WHERE before grouping and whole groups with HAVING after, route each condition by what it reads, and know why an aggregate is rejected in WHERE but legal in HAVING.
COUNT(*) vs COUNT(col)
Pick between COUNT(*), COUNT(col), and COUNT(DISTINCT col), read the gap each leaves when the column holds NULLs or repeats, and ride the same skip-NULL rule to count only the rows that meet a condition.
Subqueries & CTEs
Queries inside queries, IN, EXISTS, WITH, recursive walks
Subqueries in Filters
Substitute a subquery's result into a WHERE clause, as a single value for = or a column for IN, predict why one NULL makes NOT IN return zero rows, and rewrite the test as a join when that reads cleaner.
EXISTS & Correlated Subqueries
Wrap a correlated subquery in EXISTS to test existence row by row, explain why NOT EXISTS stays NULL-safe where a stray NULL empties NOT IN, and read the plan to see that IN and EXISTS run the same way on a large table.
CTEs & Recursive CTEs
Flatten a nested subquery into a named WITH pipeline that reads top to bottom, then walk an org chart with a recursive CTE, pairing a base case with a step that re-runs over the working table until it empties.
Window Functions
Aggregate without collapsing, OVER, PARTITION BY, ranking, running totals
OVER() & Ranking
Add OVER() to an aggregate so the rows stop collapsing, number rows with ROW_NUMBER, RANK, and DENSE_RANK, and choose among the three when two rows tie at the same value.
PARTITION BY & ORDER BY
Split a window into one partition per group so an aggregate or ranking restarts at each boundary, order rows inside each partition, and watch ORDER BY turn a flat total into one that climbs row by row.
LAG, LEAD & Frames
Reach to a row's neighbors with LAG and LEAD, draw a window's edges explicitly with ROWS BETWEEN, and watch the default RANGE frame lump tied rows where ROWS makes them climb.
Window vs GROUP BY
Choose window when you need a value beside every row and GROUP BY when you need one row per group, rewrite either form as the other, and wrap a window in an outer query when you must filter on its result.
Writes, Constraints & Transactions
INSERT, UPDATE, DELETE, constraints, ACID, isolation levels, locks
INSERT, UPDATE, DELETE
Append rows with INSERT (omitted columns fall to default or NULL), rewrite matched rows with UPDATE, remove matched rows with DELETE, and read the affected-row count, then watch a missing WHERE turn one row into the whole table.
Constraints
Declare NOT NULL, UNIQUE, CHECK, and foreign-key constraints so the table rejects a bad write itself, then choose an ON DELETE action (CASCADE, SET NULL, RESTRICT) and predict which write each one blocks.
Transactions & ACID
Wrap two writes in BEGIN/COMMIT/ROLLBACK so a crash can't strand a transfer half-done, trace the bank-transfer all-or-nothing, then map each of ACID's four guarantees to the failure it stops and see where consistency stops at the rules you actually declared.
Isolation Levels & Locks
Set an isolation level per transaction to trade concurrency for protection from dirty reads, non-repeatable reads, and phantoms, lock a row with SELECT FOR UPDATE so a concurrent writer waits, and break a deadlock by retrying the transaction the engine aborts.
How Queries Run
Indexes, EXPLAIN, join algorithms, the antipatterns that kill performance
Indexes
Trace how an index turns a full-table scan into a few-step lookup, judge when the engine skips it for a sequential scan, and apply the leftmost-prefix and covering-index rules to decide which columns to index.
EXPLAIN & the Planner
Read an EXPLAIN plan inside-out, decode each node's cost and row estimate, and use EXPLAIN ANALYZE to catch the bad row estimate behind a slow plan.
Join Algorithms
Choose between nested loop, hash, and merge join by row count, index shape, and sort order, watch why each wins a different region, and predict which one the planner will price cheapest.
Query Antipatterns
Spot the per-row loop that fires 1 + N queries, see why a leading wildcard or a function-wrapped column abandons an index, and rewrite each so the engine does only the work the result needs.