NOTE

SQL Execution Order

1. What It Is 1. Execute FROM JOIN ON and create a temporary table. 2. Execute WHERE and filter data. Aliases from SELECT cannot be used here; only fields from the FROM and JOIN tables can be used because SELECT has not executed yet. 3. Execute GROUP BY. Values that are equal are grouped together, and aggregate functions can then be used after SELECT.

DatabasesCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What It Is

1 FROM <left table>
2 ON <Join condition>
3 <join type> JOIN <right table>
4 Where <Where condition>
5 GROUP BY <group by list>
6 HAVING < having condition>
7 SELECT
8 DISTINCT <select list>
9 ORDER BY <order by condition>
10 LIMIT <limit number>
  1. Execute FROM JOIN ON and create a temporary table.
  2. Execute WHERE and filter data. Aliases from SELECT cannot be used here; only fields from the FROM and JOIN tables can be used because SELECT has not executed yet.
  3. Execute GROUP BY. Values that are equal are grouped together, and aggregate functions can then be used after SELECT.
  4. Execute HAVING to filter values within each group after grouping. Like WHERE, aliases from SELECT cannot be used.
  5. Execute SELECT and choose the fields to display.
  6. Execute DISTINCT to remove duplicate rows.
  7. Execute ORDER BY to sort. Aliases from SELECT can be used here.
  8. Execute LIMIT and OFFSET to limit the number of rows.

2. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub