Logical Query Execution Order
Why FROM runs before SELECT and how knowing the true execution order answers a dozen trick questions.
Logical Query Execution Order is a free SQL Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Question That Unlocks a Dozen Others
Interviewers ask "In what order does a query actually run?" because it explains so much: why a SELECT alias fails in WHERE, why WHERE can't filter an aggregate, and more.
Written Order vs Logical Order
You write a query starting with SELECT, but the engine runs it starting with FROM. That mismatch, with SELECT evaluated almost last, causes most confusion.
The Full Logical Order
The full logical order is: FROM, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, and finally LIMIT. Every gotcha below falls right out of this list.
Why FROM Runs First
FROM runs first because the engine needs rows to work on before anything else. It builds the source rows, and only then can WHERE decide which to keep.
SELECT e.name, d.name AS dept
FROM employees e
JOIN departments d ON d.id = e.department_id
WHERE e.salary > 50000;Gotcha: Alias Not Usable in WHERE
The most-asked consequence: a SELECT alias can't be used in WHERE, because WHERE runs before SELECT. The code below shows the error and the fix: repeat the expression.
-- FAILS: alias 'annual' unknown in WHERE
SELECT salary * 12 AS annual
FROM employees
WHERE annual > 600000;
-- FIX: repeat the expression
SELECT salary * 12 AS annual
FROM employees
WHERE salary * 12 > 600000;Gotcha: WHERE Cannot See Aggregates
WHERE runs before grouping, so it can't see aggregates like COUNT(). That's what HAVING is for: WHERE filters rows, HAVING filters groups after aggregation.
-- WHERE cannot use COUNT(*)
SELECT department, COUNT(*) AS headcount
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;Why ORDER BY Can Use Aliases
The mirror image: ORDER BY runs after SELECT, so it can use SELECT aliases. Same alias, different timing, which is why it works here but not in WHERE.
SELECT name, salary * 12 AS annual
FROM employees
ORDER BY annual DESC;Where DISTINCT Fits
DISTINCT runs after SELECT but before ORDER BY. Because it dedupes on the output columns, you can only sort by something that survived into the select list.
-- ERROR in many engines:
-- ORDER BY column not in the DISTINCT select list
SELECT DISTINCT department
FROM employees
ORDER BY hire_date;Worked Trace of a Full Query
Let's trace a real query through the steps: FROM reads the rows, WHERE filters, GROUP BY buckets, HAVING keeps groups, SELECT computes, ORDER BY sorts, LIMIT slices.
SELECT customer_id, SUM(amount) AS total
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING SUM(amount) > 1000
ORDER BY total DESC
LIMIT 10;Logical vs Physical Execution
One nuance that impresses: this is the logical order that defines correctness. The optimizer's physical plan can differ, as long as it produces the same rows.
A Memory Hook for the Order
A memory hook: picture rows flowing down a pipeline, each step handing a smaller set to the next. When something fails, check whether an earlier step produced what it needs.
Quick Check
Apply the execution order to spot the error.
Recap
Recap: the logical order is FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT. WHERE runs before SELECT (no aliases); ORDER BY runs after (aliases work).
Frequently asked questions
Is the “Logical Query Execution Order” lesson free?
Yes — the full text of “Logical Query Execution Order” is free to read here on the web, and the SQL Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Logical Query Execution Order”?
Why FROM runs before SELECT and how knowing the true execution order answers a dozen trick questions. You practise SQL Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SQL Interview Prep?
No prior experience is required. SQL Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Logical Query Execution Order” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SQL Interview Prep lesson?
Yes. Every SQL Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is SQL and Why Interviewers Ask It
- Logical Query Execution Order
- Primary Keys, Foreign Keys and Constraints
- Reading a Schema Under Pressure