
Execution Order of Statements
Introduction
When grouping, we have seen the following:
- After grouping with
GROUP BY, useHAVINGto sort, notWHERE.- The new name of a renamed column with
ASis not usable from theHAVINGstatement.
This is due to the execution order of SQL statements.
- For example,
WHEREis always executed beforeGROUP BY.- Therefore, you cannot use
WHEREafterGROUP BY. - But you must use
HAVING, which is executed afterGROUP BY.
- Therefore, you cannot use
This may seem confusing now.
The execution order is as follows:
FROMJOINWHEREGROUP BYHAVINGSELECTORDER BY
We will look at this step by step.
1. FROM

First, the FROM statement is executed.
With
FROM, you specify the table, the source, from which data should be retrieved.
Example:
FROM: We retrieve data from themoviestable.SELECT: We select all columns.
| id | name | year | rank |
|---|---|---|---|
| 10920 | Aliens | 1986 | 8.2 |
| 17173 | Animal House | 1978 | 7.5 |
| 18979 | Apollo 13 | 1995 | 7.5 |
| ... | ... | ... | ... |
2. JOIN

Next, the JOIN statement is executed (if applicable).
With
JOIN, you can merge data from different tables.
We will cover this extensively in a later module.
3. WHERE

Next, the WHERE statement is executed (if applicable).
With
WHERE, you apply one or more filters to your source data.
Example:
FROM: We retrieve data from themoviestable.WHERE: We filter for movies with a rating (rank) below8.SELECT: We select all columns.
| id | name | year | rank |
|---|---|---|---|
| 17173 | Animal House | 1978 | 7.5 |
| 18979 | Apollo 13 | 1995 | 7.5 |
| 111813 | Few Good Men, A | 1992 | 7.5 |
| ... | ... | ... | ... |
4. GROUP BY

Next, the GROUP BY statement is executed (if applicable).
With
GROUP BY, you group (aggregate) on one or more columns.
Example:
FROM: We retrieve data from themoviestable.WHERE: We filter for movies with a rating (rank) below8.GROUP BY: We group by theyearcolumn.SELECT: We select:- Column
year. - The number of rows after grouping with
COUNT(*)namedmovies_count.
- Column
| year | movies_count |
|---|---|
| 1989 | 2 |
| 1991 | 1 |
| 1984 | 1 |
| ... | ... |
5. HAVING

Next, the HAVING statement is executed (if applicable).
With
GROUP BY, you group (aggregate) on one or more columns.
Example:
FROM: We retrieve data from themoviestable.WHERE: We filter for movies with a rating (rank) below8.GROUP BY: We group by theyearcolumn.HAVING: We select only:- Results where multiple movies (
> 1) were made in a year.
- Results where multiple movies (
SELECT: We select:- Column
year. - The number of rows after grouping with
COUNT(*)namedmovies_count.
- Column
| year | movies_count |
|---|---|
| 1989 | 2 |
| 2000 | 3 |
| 2001 | 2 |
| 1999 | 2 |
Note:
In the
SELECTstatement, we rename the new column tomovies_count.This name is therefore not yet available during the
HAVINGstatement.
6. SELECT

Next, the SELECT statement is executed.
With
SELECT, you select the columns you want to retrieve.
We have seen this in the examples so far.
7. ORDER BY

Next, the ORDER BY statement is executed (if applicable).
With
ORDER BY, you sort on one or more columns.
Example:
FROM: We retrieve data from themoviestable.WHERE: We filter for movies with a rating (rank) below8.GROUP BY: We group by theyearcolumn.HAVING: We select only:- Results where multiple movies (
> 1) were made in a year.
- Results where multiple movies (
SELECT: We select:- Column
year. - The number of rows after grouping with
COUNT(*)namedmovies_count.
- Column
ORDER BY: We sort:- Descending on
movies_count-
Note: the new name
movies_countcan now be used here. -
Because the column name
movies_counthas already been created fromSELECT.
-
- Ascending on
year.
- Descending on
SELECT
year,
COUNT(*) AS movies_count
FROM
movies
WHERE
rank < 8
GROUP BY
year
HAVING
COUNT(*) > 1
ORDER BY
movies_count DESC,
year ASC;
Summary

-
SQL statements are executed in a specific order.
-
FROM- Retrieve data from a table, the source data.
JOIN- Optional: merge data from multiple tables.
WHERE- Optional: filter on the source data.
GROUP BY- Optional: group on one or more columns.
HAVING- Optional: filter after grouping.
SELECT- Selection of columns.
ORDER BY- Optional: sort on columns.