Schema design, queries, indexing, optimization
Default. Range queries, sorting, equality
Most commonEquality lookups only
O(1) but no rangeFull-text search, JSONB, arrays
PostgreSQLGeometric, range types
PostgreSQLLarge sequential tables
Block Range IndexIndex-only scans (INCLUDE)
Avoids heap lookupAtomic values, no repeating groups
1NF + no partial dependencies (all non-key cols depend on full PK)
2NF + no transitive dependencies (non-key → non-key)
Every determinant is a candidate key
Only matching rows from both tables
All from left + matching from right (NULL if no match)
All from right + matching from left
All rows from both, NULL where no match
Cartesian product (every combination)
Table joined with itself
ROW_NUMBER() Unique sequential number per partition RANK() Rank with gaps for ties DENSE_RANK() Rank without gaps LAG() / LEAD() Access previous/next row value SUM() OVER Running total NTILE(n) Divide into n equal groups SELECT DISTINCT salary FROM emp ORDER BY salary DESC LIMIT 1 OFFSET 1;
SELECT col, COUNT(*) FROM t GROUP BY col HAVING COUNT(*) > 1;
SELECT *, SUM(amount) OVER (ORDER BY date) AS running FROM orders;
WITH ranked AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) rn
FROM emp
) SELECT * FROM ranked WHERE rn <= 3;