A Deep Dive into SQL Logical Query Processing

Search for a command to run...

No comments yet. Be the first to comment.
💡 If you are here just to read about LLM streaming, jump to section 12 From first principles to production-grade architecture.Covers: protocol internals, Express server setup, multi-client forward

Here's a full deep-dive on the SWE-bench paper: PAPER X-RAY ║ Title : SWE-bench: Can Language Models ║ ║ Resolve Real-World GitHub ║

This is a Claude-generated summary! PAPER X-RAY ║ Title : SWE-agent: Agent-Computer ║ ║ Interfaces Enable Automated ║ ║

Paper: Zhang, Kraska & Khattab — MIT CSAIL, January 2026Code: github.com/alexzhang13/rlm TLDR; From first principles — before RLMs, performance degradation over large contexts was a known issue. RLM

The core question is: what makes zero-shot retrieval fail, and what would fix it? Let me build up the intuition step by step.The root problem: A user query like "how do I fix a leaky pipe?" and a docu
If you come from an imperative programming background, such as JavaScript, Python, or C++, SQL can feel counterintuitive. You define a variable, and one line later, the compiler tells you it doesn't exist.
This isn't a syntax error on your part; it is a fundamental misunderstanding of how the SQL engine parses and executes commands. To move from writing "working" queries to writing performant, production-grade queries, you need to understand Logical Query Processing.
Let’s start with the scenario that trips up almost every junior developer. You want to calculate the total value of an order and filter for high-value transactions.
Intuitively, you write this:
/* ❌ The "Imperative" Approach */
SELECT
order_id,
(quantity * unit_price) AS total_amount -- Variable defined here
FROM
orders
WHERE
total_amount > 1000; -- Variable referenced here
The Result: Error: Column 'total_amount' does not exist.
The Confusion: In JavaScript, if you declare const total = qty * priceYou can use total immediately in the next line. Why can’t SQL do the same?
Before explaining the why, here is the standard fix. You have two primary options:
Option A: Repeat the Expression
Since the alias is unavailable, you must pass the raw calculation to the filter.
SELECT order_id, (quantity * unit_price) AS total_amount
FROM orders
WHERE (quantity * unit_price) > 1000;
Option B: Common Table Expressions (CTEs)
For complex logic, calculating variables in a preliminary step (a CTE) allows you to reference them later, mimicking the imperative flow.
WITH CalculatedOrders AS (
SELECT order_id, (quantity * unit_price) AS total_amount
FROM orders
)
SELECT * FROM CalculatedOrders
WHERE total_amount > 1000;
To understand why the first query failed, we have to look at the Order of Execution.
SQL is a declarative language. You describe what result you want, and the database engine decides how to get it. However, the engine processes the clauses of your query in a strict, pre-defined sequence known as Logical Query Processing.
While you write the query in this order:
SELECT → FROM → WHERE → GROUP BY → ORDER BY
The database engine executes it in this order:
The engine begins by identifying the data source. If you are using JOINs, it creates a virtual table representing the Cartesian product of all tables involved, then filters based on the join predicates (ON). At this stage, the engine only knows about the columns that physically exist in your tables.
This is where our error occurs. The WHERE clause is applied to the rows returned from Phase 1. Its job is to discard rows that do not meet the criteria.
SELECT clause has not happened yet. The engine has not computed any derived columns, renamed any fields, or assigned any aliases. Therefore, total_amount literally does not exist in memory yet. The engine can only filter based on the raw columns (quantity, unit_price).If specified, the remaining rows are now grouped into "buckets" based on common values.
This acts like a WHERE clause, but for groups. It filters out entire buckets (e.g., "only keep groups with more than 5 items").
This is the turning point. Only after the data has been sourced, filtered, grouped, and re-filtered does the engine finally compute the expressions in your SELECT list.
This is where (quantity * unit_price) is calculated.
This is where the alias total_amount is assigned.
This explains why the alias was invisible to the WHERE clause—it hadn't been created yet.
The result set is sorted. Since this occurs after Phase 5, you can actually use aliases here.
ORDER BY total_amount DESC is perfectly valid because total_amount was created in the previous step.
You might wonder why SQL was designed this way. Why not calculate SELECT earlier?
It comes down to efficiency.
If the engine calculated (quantity * unit_price) for every single row in the database (Phase 1) before filtering them (Phase 2), it would waste massive amounts of computational power on rows that are about to be discarded anyway.
By being forced WHERE to run before SELECT, the database ensures it only performs expensive calculations on the rows that actually qualify for the final result.
This phase determines the shape of the data.
Order:
FROM →WHERE →GROUPBY →HAVING
Questions answered here:
Which tables?
Which rows?
Which groups?
Which groups are valid?
This phase formats the output.
Order:
SELECT →ORDERBY →LIMIT
Questions answered here:
Which columns?
Which calculations?
In what order?
How many rows?
SQL groups data first, then calculates aggregates, then sorts the final result.
FROM
→WHERE
→GROUPBY
→HAVING
→SELECT
→ORDERBY
→LIMIT
When writing SQL, you must mentally shift from an "Input → Process → Output" model to a "Filter → Group → Project" model.
FROM: Load the tables.
WHERE: Remove rows using raw data only.
SELECT: Compute values and name them.
ORDER BY: Sort the final output (aliases allowed).
Understanding this pipeline prevents you from fighting the database and allows you to write queries that are not just syntactically correct but also logically sound.