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.
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>
- Execute
FROM JOIN ONand create a temporary table. - Execute
WHEREand filter data. Aliases fromSELECTcannot be used here; only fields from theFROMandJOINtables can be used becauseSELECThas not executed yet. - Execute
GROUP BY. Values that are equal are grouped together, and aggregate functions can then be used afterSELECT. - Execute
HAVINGto filter values within each group after grouping. LikeWHERE, aliases fromSELECTcannot be used. - Execute
SELECTand choose the fields to display. - Execute
DISTINCTto remove duplicate rows. - Execute
ORDER BYto sort. Aliases fromSELECTcan be used here. - Execute
LIMITandOFFSETto limit the number of rows.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub