AI guide
【One-Line Pitch】
A practical bridge from everyday SQL to genuinely set-based thinking: it turns familiar clauses like CASE, HAVING, and EXISTS into sharper tools, then explains why the relational model works the way it does. Best for developers and DBAs who already write SQL but want to reason about it rather than memorize it.
【Book Arc】
- **Opening (~0%–15%)**: Starts with "unusual" uses of ordinary SQL—CASE expressions for flexible grouping and constraints, self-joins for ranking and deduplication, and the shift from row-by-row thinking to set thinking. Solves the problem of writing correct SQL for awkward reporting and ranking tasks.
- **Early (~15%–35%)**: Moves into three-valued logic and NULL, HAVING as a tool for inspecting whole sets, and outer joins for building cross-tab and horizontal layouts. Solves the "why is my result empty / wrong?" class of bugs.
- **Middle (~35%–55%)**: Covers set operations (UNION/INTERSECT/EXCEPT, set equality, relational division), EXISTS and quantified predicates, and handling ordered data such as sequences and dates. Solves problems that resist simple joins or aggregates.
- **Late (~55%–75%)**: Applies the same techniques to cumulative sums, moving averages, overlapping intervals, and other window-like calculations done without window functions. Solves time-series and interval questions in portable SQL.
- **Ending (~75%–100%)**: Shifts from technique to theory—the history of relational databases, Codd and Date's ideas, logic and set theory behind SQL, the "abolish NULL" debate, and recursion/hierarchies. Solves the "why does SQL look like this?" question and prepares readers for deeper study.
【Key Takeaways】
- **CASE is a general-purpose expression, not just a display formatter** (Opening): it can drive GROUP BY, embed logical implications in CHECK constraints, and replace rigid view-based mappings.
- **Self-joins and correlated subqueries are how SQL ranks and compares rows** (Opening): treating one table as two named sets is the mental move that unlocks ranking, deduplication, and row-to-row comparison.
- **NULL follows three-valued logic, and that changes query results** (Early): NOT IN with a NULL, ALL versus MIN/MAX, and empty-set inputs all behave counterintuitively; the book explains the truth tables behind them.
- **HAVING inspects the set, WHERE inspects the elements** (Early): this distinction is the key to questions like "which groups are entirely complete?" and to finding gaps in sequences.
- **Outer joins and scalar subqueries build cross-tabs and horizontal reports** (Early): the book shows both approaches and their trade-offs in readability and performance.
- **Set operations have algebraic properties worth knowing** (Middle): UNION and INTERSECT are idempotent, EXCEPT is not, and set equality can be tested via idempotence or one-to-one mapping.
- **EXISTS expresses quantification, including universal claims via double negation** (Middle): "all rows satisfy X" becomes "no row fails X," a pattern that generalizes to many puzzles.
- **Ordered data can be handled with sets and predicates** (Middle/Late): generating sequences, finding gaps, cumulative sums, and overlapping intervals are all done without procedural loops.
【Reading Tips】
- Read Part 1 sequentially; each section builds on the previous technique, and the exercises at section ends are worth doing rather than skimming.
- Deep-read the NULL and three-valued logic sections—they explain many real-world bugs and are referenced repeatedly later.
- Skim the historical and philosophical chapters on a first pass, then return after finishing Part 1; they make more sense once the techniques are familiar.
- Keep a scratch database handy; most examples are short enough to run and modify, which is where the learning happens.
- Treat the exercises and answers as a self-check, not optional extras—they reveal whether you actually absorbed the set-based reasoning.
【Coverage Limits】
This guide is based on stratified excerpts covering roughly the first half of the book in detail, with later chapters represented mainly by table-of-contents entries and brief mentions. Specific examples, chapter numbering, and the full depth of the relational-history chapters are not fully covered here.
Passage locations
Excerpt 1
单位有都、道、 府、县,包含一都(东京都)、 等地区为单位来分组,并统计人口数量。具体来说,就是统计下表 PopTbl 二府(京都府和大阪府)、一道 中的内容,得出如右表“统计结果”所示的结果。 (北海道)和诸多的县,统称都 道府县。多个较近的县被划归到 ■ 统计结果 B 一个地区,如关东地区、九州地 ■ 统计数...
View in text
Excerpt 2
4 关东 柠檬 100 1 关东 菠萝 100 1 关东 苹果 100 1 关东 葡萄 70 4 关西 柠檬 70 1 关西 西瓜 30 2 关西 苹果 20 3 解法还是有使用窗口函数和自连接这两种(如果把标量子查询与连接 结合起来的解法当成另一种,那就有 3 种解法)。请分别思考一下。 ●练习题 1-2-3...
View in text
Excerpt 3
1 1992 52 1993 52 1994 50 1995 50 1996 49 1997 55 ■ 年营业额的趋势 请根据这张表里的数据,使用 SQL 输出与上一年相比营业额是增加 了还是减少了,抑或是没有变化。可以先试试求出“不变”这种情况。从 表里可以看到,我们需要求出的是 1993 年和 1995 年。...
View in text
Excerpt 4
分以上,但是数学考了 40 分,所以不符合 条件。 解法是,将查询条件“所有科目分数都在 50 分以上”转换成它的双 重否定“没有一个科目分数不满 50 分”,然后用 NOT EXISTS来表示转换 后的命题。 SELECT DISTINCT student_id FROM TestScores TS1 WHER...
View in text