SQL statement execution order: 1. From tab is executed first; 2. Where statement limits the conditions; 3. Group statement [group by... having]; 4. Aggregation function; 5. Select statement ;6. Order by sorting statement.
The operating environment of this article: Windows 7 system, sql server 2019 version, Dell G3 computer.
sql statement execution sequence:
1. from
First determine which table to fetch data from, so from tab is executed first. There are multiple table connections from tab1, tab2. You can add aliases to the table to facilitate subsequent references.
2. Where
The where statement limits the condition. If there is no need to limit it, write where 1=1, which means it is always true. , no strings attached.
3. group by... having
Grouping statements, such as grouping by employee name, the field to be grouped must appear in the select, otherwise An error will be reported. Having is used in conjunction with group by to limit conditions. Here is an example.
4. Aggregation functions
Commonly used aggregation functions include max, min, count, and sum. Aggregation functions are executed after group by and before having. If you write an aggregate function in where, an error will occur.
5. Select statement
Select the field to be searched. If you select all, you can select *. Here select the employee name and total salary for all months.
6. Order by
sorting statement, the default is ascending order. If you want to sort in descending order, write order by [XX] desc. The order by statement is executed at the end, and sorting can only be performed if select selects the field to be found.
Recommended (free): sql tutorial
The above is the detailed content of What is the order of execution of sql statements?. For more information, please follow other related articles on the PHP Chinese website!