← All mindmapsMAIN TOPIC · 03 / 14SQL & Databases
Query execution, joins, relational design and transactions
Explore full topic ↗ Readable revision notes (18 concepts)
The same content as the visual sheet, optimized for mobile and assistive technology.
01 · Relational model
- Primary key identifies row; foreign key references key
- UNIQUE constrains duplicates; NULL means unknown
- Normalize to reduce redundancy; denormalize deliberately
02 · SQL essentials
- SELECT columns FROM table WHERE condition
- ORDER BY sorts; LIMIT restricts rows
- INSERT / UPDATE / DELETE modify data; use parameters
03 · Joins
- INNER: matched rows only; LEFT: all left rows
- RIGHT: all right rows; FULL: unmatched from both
- CROSS: Cartesian product; ON controls matching
04 · Aggregations
- GROUP BY forms groups; HAVING filters groups
- COUNT(*) counts rows; COUNT(col) ignores NULL
- SUM, AVG, MIN, MAX; DISTINCT removes duplicates
05 · Subqueries & windows
- CTE: WITH name AS (...); EXISTS tests existence
- OVER(PARTITION BY ... ORDER BY ...) preserves rows
- ROW_NUMBER unique order; RANK gaps; DENSE_RANK no gaps
06 · NULL & predicates
- IS NULL; comparisons with NULL yield UNKNOWN
- COALESCE(a,b) returns first non-null value
- NOT IN with NULL can surprise; consider NOT EXISTS
07 · Indexes & performance
- B-tree supports equality/range; indexes cost writes
- Composite indexes: leading keys often matter
- EXPLAIN ANALYZE runs query and shows actual plan
08 · Transactions & ACID
- Atomicity, consistency, isolation, durability
- BEGIN → operations → COMMIT / ROLLBACK
- MVCC snapshots; isolation levels trade concurrency/safety
09 · Security & integrity
- Use prepared statements; least privilege; RLS
- Avoid N+1 queries; paginate stable ordering
- Deadlocks: retry transaction; order locks consistently
10 · Logical SQL execution
- WHERE filters input rows before grouping; HAVING filters groups.
- SELECT aliases generally unavailable to WHERE; dialect differences apply.
- Window calculations occur after grouping; subquery/CTE to filter window results.
11 · Join cardinality
- INNER keeps matches; LEFT retains all left records with NULL on unmatched right.
- One-to-many matches multiply rows; duplicates are not automatically errors.
- Filtering right table in WHERE after LEFT JOIN can remove unmatched rows.
12 · Window functions
- ROW_NUMBER unique sequence; RANK leaves gaps; DENSE_RANK no gaps.
- LAG/LEAD access neighbor rows; SUM OVER creates running total.
- Use explicit ROWS frame for row-by-row cumulative sum; deterministic tie-breakers.
13 · Top-N & deduplication
- ROW_NUMBER() OVER(PARTITION BY key ORDER BY ts DESC,id DESC).
- Filter rn=1 in outer query for latest row per group.
- Top k per group: rn<=k; rank-based logic includes ties.
14 · Normalization
- 1NF atomic attributes; 2NF removes partial key dependencies.
- 3NF removes problematic transitive dependencies; BCNF every determinant superkey.
- Use junction table for many-to-many; denormalize after measurement.
15 · Indexes & query plans
- B-tree indexes ordered keys; composite order matters; unique index enforces uniqueness.
- EXPLAIN shows estimated plan; EXPLAIN ANALYZE executes query.
- Seq scan may be optimal for large fraction; compare actual rows and buffers.
16 · Isolation & anomalies
- Read committed may see different snapshots per statement (PostgreSQL).
- Repeatable read uses stable snapshot; serializable may abort conflicting transactions.
- Lost updates: atomic updates/locks; retry serialization failures safely.
17 · NoSQL vs SQL
- Relational for constraints, joins, transactions; document for flexible aggregate shape.
- Key-value for simple lookup; graph for relationship traversal.
- Choose by access patterns, consistency, operational needs; not by data size alone.
18 · Production SQL
- Parameterize input; avoid string interpolation; least privilege and RLS.
- Keyset pagination on stable ordered keys; avoid high OFFSET where costly.
- Transactions for multi-step writes, migrations backward-compatible, backup and restore test.
References & further study
Original schematics illustrate concepts, not measured data. Review these references for full derivations and implementation details.