First, summary statistics
1, aggregate functions
COUNT () calculates the total number of
the SUM () sum
MAX () maximum value
MIN () minimum
the AVG () average
2, aggregate functions using
Total number of students? COUNT the SELECT ( * ) the FROM Student; sum math for all students? SELECT SUM (score) FROM student; all students in math highest score? SELECT MAX (score) FROM student; all students in math lowest score? SELECT MIN (score) FROM student; all students in math average? AVG the SELECT (Score) the FROM Student; ### the number of students? COUNT the SELECT ( *) the FROM Student; 219 another way: the SELECT COUNT (Score) the FROM Student; 219 the SELECT COUNT (the student_id) the FROM Student; 219 the SELECT COUNT ( . 1) the FROM Student; 219 the SELECT COUNT ( 'the ABC') the FROM Student ; 219 COUNT the SELECT (teacher_id) the FROM Student; 205 COUNT (teacher_id) ignored null value row; the same row is the NULL value is ignored: the SELECT MAX (Score) the FROM Student; the SELECT MIN (Score) the FROM Student; the SELECT the SUM (Score ) the FROM Student; the SELECT AVG (Score) the FROM Student; COUNT ( *) and COUNT (1 ) comparison: If your table has no primary key, then the COUNT ( 1) than COUNT (* ) fast if there is a primary key, then the primary key ( the primary key) as a condition of count than the count ( * ) to be fast if your table has only one field, then it count ( * ) is the fastest of friends count ( compare both *) count (1). Mainly to COUNT (1 ) of the corresponding data fields. If COUNT ( 1) is a poly index, id, it is certainly COUNT (1 ) fast. But the difference is very small. Because the count (*), Automatically optimizes designated to that field. So no need to count (?), With COUNT (* ), will help you complete the SQL optimization. AVG (score) is equivalent to the SUM (Score) / COUNT (Score)
3, group summary statistics
Keywords: GROUP BY , such as: how to get each math class average (because too many students, not counting here the first students in grades 80 points or less) in order to compare the results of different classes? Class_id the SELECT, AVG (Score) the WHERE the FROM Student Score > 80 GROUP BY class_id; Note : SELECT fields in addition to using the aggregate function returns, all the other fields as they must appear after the GROUP BY;
For example: how the statistics of the total purchase amount of different exchanges? Left the SELECT (stock_code, 2), SUM (. Price * Volume) the FROM t_stock_trans_dtl the WHERE opt_typ = 'buy' the GROUP BY left (stock_code, 2 ); for example: how the statistics of the different types of transactions the total transaction amount of different exchanges? The field of the packet # plurality of the SELECT left (stock_code, 2), opt_typ, SUM (. Price * Volume) t_stock_trans_dtl the FROM the GROUP BY left (stock_code, 2 ), opt_typ; row will be NULL value GROUP BY, assigned to the same set of statistical calculation value; the SELECT teacher_id, COUNT ( * ) the FROM Student the GROUP BY teacher_id;
Second, summary statistics
1, packet filtering results
Keywords: the GROUP BY + the HAVING HAVING specific keywords used to group by specifying conditions; HAVING comparison and where: HAVING: packet filtering, performed after GROUP BY; where: line filtering is performed before the GROUP BY; example: How Professor acquired more than 15 scientific name of the teacher? Teacher_id the SELECT, COUNT ( * ) the FROM Student the GROUP BY teacher_id the HAVING COUNT ( *)> 15 ; for example: how to get the average score in math class and 70 points or more? The SELECT class_id, AVG (Score) the FROM Student the GROUP BY class_id the HAVING AVG (Score) > = 70;
2, the packet sorting results
For example: How to get math scores in each class average (not counting the students in math 80 points or less), and then sorted in descending average? Keyword: the GROUP BY + the ORDER BY # will first perform where, then the Order by the SELECT class_id, AVG (Score) the FROM Student the WHERE Score > 80 the GROUP BY class_id the ORDER BY AVG (Score) DESC ; If you want to determine every query sort the results, you must use the ORDER BY !
3, SELECT clause order
The most simple SQL statement:
SELECT 1;