Loading
SQL Execution Order
When we write an SQL query, we usually write it in this order 

SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
LIMIT ...

But SQL does not execute the query in this order. This is one of the most confusing concepts for beginners.

Many errors, wrong results, and misunderstandings happen because:

  • People think SQL runs line by line
  • They assume SELECT is executed first
In reality, SQL follows its own internal execution workflow.

Understanding this workflow is critical for:

  • Writing correct queries
  • Using WHERE, GROUP BY, and HAVING properly
  • Avoiding logical mistakes


What Is SQL Execution Order?

The SQL execution order defines the sequence of steps that the database follows internally to process a SELECT query.

SQL first decides where the data comes from,
then which rows to keep,
then how to group and filter,
and only at the end what do display.


General Execution Order of a SELECT Query

SQL executes a SELECT query in the following order:

1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
7. LIMIT / OFFSET


1. FROM Clause – Data Source Selection

What Happens Here?

  • SQL identifies which table(s) to read data from
  • JOINs are processed at this stage
  • A raw dataset is created
FROM employees

At this point, no filtering is applied.


2. WHERE Clause – Row Filtering

What Happens Here?

  • SQL evaluates conditions row by row
  • Rows that do not match the condition are discarded
WHERE department = 'IT'

  • WHERE works only on individual rows
  • WHERE cannot use aggregate functions


3. GROUP BY Clause – Group Formation

What Happens Here?

  • Rows are grouped based on column values
  • Each unique value becomes one group
GROUP BY department

Without GROUP BY, aggregate funtions work on the entire table.


4. HAVING Clause – Group Filtering

What Happens Here?

  • Aggregate functions are applied to each group
  • Groups that do not satisfy the condition are removed
HAVING AVG(salary) > 50000

HAVING works on groups, not rows


5. SELECT Clause – Column Selection

What Happens Here?

  • SQL selects the columns to display
  • Aggregate functions are calculated
  • Aliases are applied
SELECT department, AVG(salary) AS avg_salary

This is why aliases cannot be used in WHERE, but can be used in ORDER BY.


6. ORDER BY Clause – Sorting Results

What Happens Here?

  • The final result set is sorted
  • Sorting happens after SELECT
ORDER BY avg_salary DESC

Aliases are allowed here


7. LIMIT / OFFSET – Restricting Rows

What Happens Here?

  • SQL restricts the number of rows returned
  • Used for pagination and Top-N queries
LIMIT 5 OFFSET 0


Example: Execution Flow in Action

SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING AVG(salary) > 50000
ORDER BY avg_salary DESC
LIMIT 3;

Execution Flow

1. FROM - employees table selected
2. WHERE - salaries > 30000 filtered
3. GROUP BY - grouped by department
4. HAVING - avg salary condition applied
5. SELECT - columns and alias created
6. ORDER BY - sorted by avg_salary
7. LIMIT -top 3 results returned


Why Understanding Execution Order Is Important

  • Prevents logical errors
  • Explains why WHERE and HAVING behave differently
  • Helps write optimized queries
  • Essential for interviews and MCQs


Two Minute Drill

  • SQL does not execute queries in written order
  • FROM is executed first
  • WHERE filters rows
  • GROUP BY forms groups
  • HAVING filters groups
  • SELECT decides output
  • ORDER BY sorts results
  • LIMIT restricts rows