DatabasesDeeper

Kayavak / Databases / DB·56 SQL from SELECT to Window Functions / Deeper

Deeper · Databases

SQL from SELECT to Window Functions

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Codd's relational algebra was mathematically precise and hard for most people to write. At IBM in 1974 Donald Chamberlin and Raymond Boyce designed SEQUEL, a "structured English query language" in which a query reads roughly like a sentence; a trademark clash renamed it SQL. It was meant for analysts as much as programmers, and it was declarative: you state the result, not the steps.

SQL kept growing through standards: joins written explicitly (SQL-92), recursive common table expressions (SQL:1999), window functions (SQL:2003), and later JSON and row-pattern matching. Window functions in particular turned many problems that once needed self-joins or application loops (running totals, rankings, comparisons with the previous row) into single queries.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

A SQL query describes a result as a pipeline of set operations: start from the joined rows, filter, group, compute, sort, limit. The written order isn't the logical order, and the planner is free to execute it any way that gives the same result.

Question Answer
What is it? A declarative language for querying and changing relational data
Problem it solves Asking any question of the data without writing the algorithm to answer it
Parts SELECT, joins, WHERE, GROUP BY and HAVING, subqueries, CTEs, window functions, set operations, DML
How it works Logically: FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT and windows → DISTINCT → ORDER BY → LIMIT
Assumptions You know which rows a join multiplies and which a filter removes
When they change Joining two one-to-many tables multiplies rows (fan-out), so sums count things twice
Where it fails Procedural logic and very complex transformations become unreadable as one statement
Related ideas Relational model (ch55); query planning (ch60); data engineering transformations (Data Engineering)

What if?

What if you join orders to both their items and their payments, then sum the totals?

Reason it through, then open

An order with 3 items and 2 payments produces 3 × 2 = 6 joined rows, so summing item prices counts each item twice and summing payments counts each payment three times. Aggregate each child table separately (in subqueries or CTEs) and then join the results, one row per order.

What if SQL ran in the order it's written?

Reason it through, then open

SELECT comes first in the text, but it can't run first: it needs rows from FROM and filtering from WHERE. That's why you can't use a column alias from SELECT in WHERE, but can in ORDER BY, which logically runs after it. Knowing the logical order explains most "why can't I write this?" errors.

Where the boundary is

Aspect GROUP BY Window function
Rows out One per group One per input row
Can see The group's aggregate The aggregate and each row's own values
Example Total sales per day Each sale with its day's running total

How sure are we?

Claim How it's known Where from
SEQUEL was designed by Chamberlin and Boyce in 1974 Historical Their 1974 paper
Logical evaluation order of a SELECT Convention The SQL standard's semantics; the planner may execute differently
Window functions arrived in SQL:2003 Historical ISO/IEC 9075:2003

Still open

SQL's syntax is widely criticised (clause order, verbosity), and alternatives such as pipe syntax (Google, 2024) and PRQL propose writing queries in the logical order; whether any displaces SQL is open.

What would change your mind? If a new query language offered the same declarative power with clearer syntax and became widely supported, SQL's position would be mainly inertia. Inertia has kept it here for fifty years.

Recall

What's SQL's logical evaluation order?

FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT (and windows), DISTINCT, ORDER BY, LIMIT.

What is join fan-out?

Joining to several one-to-many children multiplies rows, inflating aggregates.

How does a window function differ from GROUP BY?

It computes over a group but keeps one output row per input row.

Why can ORDER BY use a SELECT alias but WHERE can't?

ORDER BY runs after SELECT logically; WHERE runs before it.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.