Learn With Grito

SQL Query Execution Order
Explained for Beginners

Most beginners read SQL queries from top to bottom. Seems logical. But databases do not execute SQL in that order. That is why beginners constantly get confused by errors.

Tutorial Series8 Mins ReadSQL Level 10

This is where SQL starts becoming truly understandable. Most beginners read SQL queries from top to bottom.

Written Order
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

Seems logical. But databases do not execute SQL in that order. That is why beginners constantly get confused by errors like:

  • “column does not exist”
  • “aggregate function not allowed here”
  • “invalid grouping”
  • “unknown alias”

If you understand SQL query execution order, many confusing SQL problems suddenly make sense. For data analysts, this is one of the highest-ROI SQL concepts to learn early.

What Is SQL Query Execution Order?

SQL query execution order is the logical sequence in which the database processes a query.

Important: This is not the same as the order in which you write SQL.

Example written query
SELECT city, SUM(revenue) AS total_revenue
FROM orders
WHERE status = 'Completed'
GROUP BY city
HAVING SUM(revenue) > 50000
ORDER BY total_revenue DESC;

You wrote: 1. SELECT, 2. FROM, 3. WHERE, 4. GROUP BY, 5. HAVING, 6. ORDER BY. But SQL logically processes it differently.

Simple idea: SQL is written one way, executed another way.

Why SQL Query Execution Order Matters for Data Analysts

Because analysts constantly write multi-step queries. And many SQL mistakes come from misunderstanding execution flow.

Real examples:

  • Why can't I use SUM() in WHERE?
  • Why does my alias fail in WHERE?
  • Why does GROUP BY throw errors?
  • Why does HAVING work but WHERE doesn't?

All of these become obvious once execution order is clear.

SQL Written Order vs Execution Order

Written order:

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

Logical execution order:

FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY

This is the beginner version.

More complete order:

  • 1. FROM
  • 2. JOIN
  • 3. WHERE
  • 4. GROUP BY
  • 5. HAVING
  • 6. SELECT
  • 7. DISTINCT
  • 8. ORDER BY
  • 9. LIMIT / TOP

This is the version analysts should remember. Let’s break this down step by step.

1. FROM

Execution starts with FROM. SQL first identifies the source table.

Example
SELECT customer_name
FROM customers;

Step 1: Locate customers. Without data source, nothing else happens.

2. JOIN

If joins exist, SQL combines tables next.

Example
SELECT c.customer_name, o.revenue
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;

Execution: Step 1: Load customers. Step 2: Join orders. Only then does filtering happen. Why this matters? Because filtering can happen after joined data exists. This explains many multi-table query behaviors.

3. WHERE

WHERE filters rows.

Example
SELECT *
FROM orders
WHERE revenue > 10000;

Important Rule: WHERE works before grouping. That means: WHERE filters individual rows. Not grouped data.

4. GROUP BY

After filtering, SQL groups remaining rows.

Example
SELECT city, COUNT(*)
FROM customers
GROUP BY city;

Rows become grouped by city.

5. HAVING

HAVING filters grouped data.

Example
SELECT city, COUNT(*)
FROM customers
GROUP BY city
HAVING COUNT(*) > 100;

WHERE vs HAVING WHERE: filters rows. HAVING: filters groups. This is a massive beginner confusion point.

6. SELECT

Only now does SQL decide what to return. Yes. SELECT happens surprisingly late.

By this point: rows already loaded, joins done, filters applied, grouping completed. Now SQL builds output.

Alias Confusion
-- Fails:
SELECT revenue AS total_revenue
FROM orders
WHERE total_revenue > 10000;

Why? Because WHERE executes before SELECT. Alias does not exist yet.

The Grito Factor The most misleading thing about SQL is that the first word in your query often executes near the end. That single fact explains why thousands of beginners think SQL is “weird” when it is actually extremely logical.

7. DISTINCT

DISTINCT removes duplicate rows after SELECT output is formed.

8. ORDER BY

Sorting happens late.

Why This Matters: ORDER BY can often use aliases.

Example
SELECT revenue AS total_revenue
FROM orders
ORDER BY total_revenue DESC;

Works. Because ORDER BY happens after SELECT.

9. LIMIT / TOP

Final step. Restrict result count.

Visual Memory Trick

Think: Find → Filter → Group → Filter Groups → Show → Sort → Trim

StepSQL Clause
Find dataFROM
Combine tablesJOIN
Filter rowsWHERE
Group rowsGROUP BY
Filter groupsHAVING
Build outputSELECT
Remove duplicatesDISTINCT
SortORDER BY
Limit rowsLIMIT

This is a great interview memory shortcut.

Common Beginner Mistakes

1. Using Alias in WHERE

Wrong: WHERE total_revenue > 5000
Alias not created yet.

2. Using Aggregate in WHERE

Wrong: WHERE SUM(revenue) > 10000
Aggregation happens later. Correct: HAVING SUM(revenue) > 10000

3. Confusing WHERE and HAVING

Very common. WHERE = rows, HAVING = groups.

4. Thinking SELECT Executes First

It looks first. It is not first.

5. Misunderstanding ORDER BY Alias Behavior

This works: ORDER BY total_revenue. Because ORDER BY happens after SELECT.

SQL Query Execution Order in Interviews

Extremely common interview topic.

Questions:

  • What is SQL query execution order?
  • Why does alias fail in WHERE?
  • WHERE vs HAVING?
  • Why can't aggregate functions go in WHERE?
  • Does ORDER BY happen before SELECT?
  • Why can ORDER BY use aliases?

This topic strongly differentiates strong candidates.

Practice Questions

  1. Which clause executes first?
  2. Which clause filters rows?
  3. Which clause filters grouped results?
  4. Why does alias fail in WHERE?
  5. Which clause executes last?

What Comes Next?

Now that SQL execution order makes sense, we move into actual query building. Next: SQL SELECT Statement Explained. This is where practical querying begins.

Final Thoughts

SQL query execution order is one of the most important beginner breakthroughs.

Once this clicks:

  • WHERE vs HAVING becomes obvious
  • alias behavior makes sense
  • grouping errors become easier
  • complex SQL becomes less intimidating

For data analysts, this concept pays off constantly. Master it early.

Grit Over Excuses.

— The Grito Team