1, where, and having keywords
where and having filter conditions can be used in the sql statement, group by statement and can be used together.
Two differences:
(1) when used with group by, where before the group by statement, where the representative first filtered with a statement, and then use the results of the group by filtration packets;
And having statements can only be used after the group by, the result grouped for screening.
After the condition (2) where the expression is not allowed to use a polymerization function , when the need to use a polymerization function can only be on the back having.
Exercise sql: query average score less than 60 points and the number of students in the student's name and student grade point average - (including performance and non-performance)
--学生表 CREATE TABLE `Student`( `s_id` VARCHAR(20), `s_name` VARCHAR(20) NOT NULL DEFAULT '', `s_birth` VARCHAR(20) NOT NULL DEFAULT '', `s_sex` VARCHAR(10) NOT NULL DEFAULT '', PRIMARY KEY(`s_id`) ) --成绩表 CREATE TABLE `Score`( `s_id` VARCHAR(20), `c_id` VARCHAR(20), `s_score` INT(3), PRIMARY KEY(`s_id`,`c_id`) );
The SELECT a.s_id student numbers, ROUND ( AVG (a.s_score), 2 ) grade point average, b.s_name student name the FROM Score A LEFT JOIN Student b ON a.`s_id` = b.`s_id` the GROUP BY a.s_id , b.s_name the HAVING AVG (a.s_score) < 60 UNION the SELECT a.s_id student number, 0 grade point average, a.s_name student name the FROM student A the WHERE a.`s_id` the NOT the IN ( the SELECT DISTINCT score.s_id the FROM score)