How SQL Query Engines Actually Execute Queries: The Complete Lifecycle
By Sharon Ben-Moshe · October 13, 2026 · 2 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Journey through the SQL query lifecycle. Explore lexical parsing, Abstract Syntax Trees, logical optimization, Cost-Based Optimizers, and Volcano execution.
Quick answer
SQL is a declarative language: you specify WHAT data you want, not HOW to retrieve it. The database engine executes queries through a five-stage pipeline: 1) Parser & Lexer (generates the Abstract Syntax Tree), 2) Semantic Analyzer (validates schemas and resolves types), 3) Rewriter (expands views and folds constants), 4) Cost-Based Optimizer (evaluates statistical costs to choose physical algorithms), and 5) Execution Engine (processes rows via Volcano iterator or vectorized models).