1. Introduction to aggregate function
Aggregation function is also called group function, which usually counts and calculates the data in the table. It is generally used in combination with group by for statistics and calculation of group data.
Common aggregate functions:
① , count (col): calculate the total number of rows of the specified column
② , Max (col): to calculate the maximum value of the specified column
③ , Min (col): to find the minimum value of the specified column
④ Sum (col): sum the specified column
⑤ , Avg (col): calculate the average value of the specified column
2. Query the number of students
Syntax: SELECT COUNT (` id ') FROM ` students';
Note: The aggregate function does not count null values. Generally, if you want to specify a column name, it is a primary key field.
General writing method: SELECT COUNT (*) FROM ` students';
3. Query the oldest student of a girl
语法:SELECT MAX(age) FROM `students` WHERE `gender` = ‘女’;
4. Query the average height of male students
语法:SELECT avg( `height`) FROM `students` WHERE `gender` = ‘男’;