SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
Among them, SELECT and FROM are required, and other keywords are optional.
2. The writing order of these keywords:
SELECT field list
FROM table name
WHERE record filter condition
GROUP BY grouping field list
HAVING grouping filter condition
ORDER BY sorting field list
3. The difference between WHERE and HAVING:
WHERE filters records; HAVING It is to filter the groups grouped by GROUP BY.
HAVING can only be used after using GROUP BY.
4. The two functions of GROUP BY:
the first function is to group the records taken from the table according to one or more fields;
the second function is to remove duplicates, and its essence is still grouping.
5. The parsing/execution order of these keywords:
FROM->WHERE->GROUP BY->HAVING->SELECT->ORDER BY
Specifically:
FROM: Assemble records from different data sources
WHERE: filter the data according to the specified conditions Records retrieved in one step
GROUP BY: Group the filtered records above according to specified conditions
SUM/AVG/COUNT: Use aggregate functions to calculate
HAVING : filter all groups according to the specified conditions
SELECT: extract the field list of the specified query (including aggregate fields, calculation fields, expression fields, etc.)
The result set is sorted according to the list of sorting fields, and the sorting results are output.
6. GROUP BY field 1, field 2 - first group all records according to field 1, and then group the grouped results according to field 2 in each group.
7. ORDER BY field 1, field 2 - first sort by field 1, and then sort by field 2.
8. Collation keywords: ASC (ascending, default), DESC (descending).
9, COUNT keyword, used for counting. COUNT(*) is the overall count of the candidate dataset, and COUNT(field) is the count of a field. If there is a NULL value in a column, COUNT(*) will be included in the column, and COUNT(field) will not be included in the column.
Example:
In the student score table, group the scores of students in class 1 according to their names, filter the results of the groupings, summarize and calculate the total scores of each student, select a list of students with a total score greater than 600 points, and classify them according to the total score from Arranged high to low.
The SQL statement is as follows:
select std_name, sum(score) as sum_score from grade_table where class_id=1 group by std_name having sum(score)>600 order by 2 asc
In the above example, the execution order of the SQL statement is as follows:
① First execute the FROM clause to retrieve records from the grade_table table;
② Execute the WHERE clause to filter out all records with class_id=1 in the grade_table table;
③ Execute the GROUP BY clause, Group the grade_table table according to the std_name column;
④ Calculate the SUM() aggregate function to find the sum of the scores of each std_name;
⑤ Execute the HAVING clause to filter out the groups whose sum(score) is greater than 600;
⑥ Extract std_name, sum( score) two fields to generate a new result set;
⑦ Execute the ORDER BY clause, and sort the result set of ⑥ in ascending order by the sum (score) field.
References:
[1] The execution order of select from where group by having order by in the query statement
[2] The explanation and execution order of the sql keyword
[3] The execution order of the sql keyword
Thank you !
If there are any mistakes or inappropriate descriptions in the text, please correct me!
Author: Flying Lion Sanduo Email: [email protected] If there is anything inappropriate, please correct me.