80%
DefinitionA relational database stores data in tables with a fixed schema, linked by keys, queried with SQL, and protected by ACID transactions (MySQL, PostgreSQL, SQL Server…
FlowLogical order SQL runs in — why WHERE can't use a SELECT aliasFROM / JOIN→WHERE→GROUP BY→HAVING→SELECT→ORDER BY→LIMIT
Primary key: unique, not null row id.
WHERE filters rows before grouping; HAVING filters groups after.NULL means unknown: use IS NULL, never = NULL; COUNT(col) skips NULLs, COUNT(*) doesn't.INNER JOIN: only matching rows in both tables
LEFT JOIN: all rows from the left table, NULLs where there's no match
RIGHT JOIN: all rows from the right table
FULL OUTER JOIN: all rows from both
Example: GROUP BY, HAVING, subquery
DefinitionWindow functions compute across related rows without collapsing them (unlike GROUP BY):
fn() OVER (PARTITION BY … ORDER BY …).LAG() / LEAD() look at the previous / next row (month-over-month change).Running total:
SUM(amount) OVER (ORDER BY day).CTEs (
WITH …) name subqueries; recursive CTEs walk hierarchies.DefinitionAn index (usually a B-tree) lets the database find rows without scanning the whole table — faster reads, slightly slower writes and extra storage.
Index columns used in WHERE, JOIN and ORDER BY; high-selectivity columns first.
Composite index (a, b) helps
WHERE a = ? and WHERE a = ? AND b > ?, but not WHERE b = ? alone (leftmost-prefix rule).Functions on a column (
WHERE LOWER(email) = …) skip a normal index.Use
EXPLAIN / EXPLAIN ANALYZE to see scans vs index use; avoid SELECT *; fix N+1 queries with joins or batching.ACID: Atomic (all or nothing), Consistent (constraints hold), Isolated (concurrent transactions don't interfere), Durable…
Read uncommitted: nothing (dirty reads possible)
Read committed (Postgres default): dirty reads
Repeatable read (MySQL InnoDB default): + non-repeatable reads
Serializable: + phantom reads (safest, slowest)
1NF: atomic values, no repeating groups (no "tags: a,b,c" column)
2NF: 1NF + every column depends on the whole primary key
3NF: 2NF + no column depends on another non-key column
Many-to-many needs a junction table (
student_course(student_id, course_id)).Denormalise deliberately for read-heavy reporting (with care to keep copies in sync).
Constraints keep data correct:
NOT NULL, UNIQUE, CHECK, FOREIGN KEY … ON DELETE CASCADE/RESTRICT.Soft delete (
deleted_at) when you need history or undo.