Cheat sheets

DB-08Cheat sheet1 pageIn a paid pack

SQL: Joins, Duplicates, Set Operations

DDL, DML, DCL and TCL statements; DELETE versus TRUNCATE versus DROP; a table definition with every constraint kind; the logical evaluation order of a SELECT; inner, outer, cross and natural joins counted on one instance, including the left join whose WHERE filter silently turns it into an inner join; duplicates and DISTINCT; UNION, INTERSECT and EXCEPT with their ALL forms and the bag counting rules; LIKE, BETWEEN and NULL sort order. Every result was run on SQLite, and INTERSECT ALL and EXCEPT ALL on PostgreSQL.

DB-09Cheat sheet1 pageIn a paid pack

SQL: NULLs, Aggregates, GROUP BY

Three-valued logic with its AND, OR and NOT tables; comparisons and arithmetic with NULL; which clauses keep TRUE only and which reject only FALSE; every aggregate on one small table with NULLs, including AVG versus SUM divided by COUNT(*), COUNT(column) and COUNT(DISTINCT); aggregates and GROUP BY on empty input; NULLs as one group; the SELECT-list rule for GROUP BY; WHERE versus HAVING; HAVING without GROUP BY. All results were run on SQLite and PostgreSQL.

DB-10Cheat sheet1 pageIn a paid pack

SQL: Nested and Correlated Subqueries

Scalar, row and table subqueries; uncorrelated versus correlated evaluation; EXISTS, IN, NOT IN, ANY and ALL with their exact truth values when the subquery is empty or contains NULL; the equivalences = ANY with IN and <> ALL with NOT IN, and why > ALL is not > MAX; maximum and second maximum without MAX; "for all" with double NOT EXISTS; IN versus JOIN duplicates; and a checked past GATE query. Every result was run on PostgreSQL 17 and, where supported, SQLite.