Skip to main content

Order of execution of a SQL Query

Considering the following SQL

SELECT DISTINCT column, AGG_FUNC(column_or_expression),
FROM mytable
JOIN another_table
ON mytable.column = another_table.column
WHERE constraint_expression
GROUP BY column
HAVING constraint_expression
ORDER BY column ASC/DESC
LIMIT count OFFSET COUNT;

The order of execution is:

  1. FROM and JOIN
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. DISTINCT
  7. ORDER BY
  8. LIMIT and OFFSET