Difference and usage between GROUP BY, WHERE, HAVING

The having clause is similar but different from where, both of which are statements that set conditions.
In the query process, the aggregation statement (sum, min, max, avg, count) is executed prior to the having clause. The where clause is executed prior to the aggregation statement (sum, min, max, avg, count) in the query process. ).
To put it simply:
where clause:
select sum(num) as rmb from order where id>10
//Only the records with id greater than 10 can be queried before aggregation statement
having clause:
select reports to manager, count(*) as reports from employees
group by reportsto having count(*) > 4
Take the northwind library as an example. The having condition is expressed as an aggregate statement. It is certain that the query process of the having clause has a lower priority than the aggregation statement.
In other words, it will be an error to replace the having above with where. Aggregate statements are used to count grouped data.
To use having when judging the grouped data again. If you don't use these relationships, there is no use of having. Just use where.
Having is to make up for the lack of where in the judgment of grouped data. Because the where execution priority is faster than the aggregate statement.
Aggregate function, which is a special function that must be mentioned first:
such as SUM, COUNT, MAX, AVG, etc. The fundamental difference between these functions and other functions is that they generally act on multiple records.
SELECT SUM(population) FROM tablename
The SUM here acts on the population field of all returned records. The result is that the query only returns one result, that is,
the total population of all countries. By using the GROUP BY clause, the functions SUM and COUNT can be made to work on data belonging to a group.
When you specify GROUP BY region, a group of data belonging to the same region (region) will only return one row of values.
That is to say, all fields except region (region) in the table can only return a value after operation by aggregate functions such as SUM and COUNT.
The HAVING clause allows us to filter groups of data.
The HAVING clause filters group records after aggregation
while the WHERE clause filters records before aggregation. That is to say, before the GROUP BY clause and the HAVING clause,
look at the following examples:
1. Display the total population and total area of ​​each region.
SELECT region, SUM(population), SUM(area)
FROM bbc
GROUP BY region
First divide the returned records into multiple groups by region, which is the literal meaning of GROUP BY. After grouping, aggregate functions are then used to operate on the different fields (one or more records) in each group.
2. Display the total population and total area of ​​each region. Only those areas with an area greater than 1,000,000 are shown.
SELECT region, SUM(population), SUM(area)
FROM bbc
GROUP BY region
HAVING SUM(area)>1000000
Here, we cannot use where to filter regions with more than 1,000,000, because there is no such record in the table.
Conversely, the HAVING clause allows us to filter groups of data.

The database used by the following examples is MySQL 5.
Data table: student
table structure:
Field Name DataType Len
id int 20
name varchar 25
major varchar 25
score int 20
sex varchar 20
table data:
number/name/major/credit/sex
id name major score sex
1 jak Chinese 40 f
2 rain Math 89 m
3 leo Phy 78 f
4 jak Math 76 f
5 rain Chinese 56 m
6 leo Math 97 f
7 jak Phy 45 f
8 jak Draw 87 f
9 leo Chinese 45 f
Now we want to get a view:
ask the gender to be male , and list the total score of each student:
SQL:
select s.*,sum(s.score) from student s where sex='f' group by s.name
Result:
id name major score sex sum(s.score)
1 jak Chinese 40 f 248
3 leo Phy 78 f 220
It can be seen that there are two groups of students in total. The students in the two groups are jak and leo, and each group is the same students, so we can use aggregate functions.
Aggregate functions such as count() and sum() can only be used if the group by statement is used.
Next, we will further filter the above results and only display students with a total score greater than 230:
SQL:
select s.*,sum(s.score) from student s where sex='f' group by s.name having sum (s.score)>230
Result:
id name major score sex sum(s.score)
1 jak Chinese 40 f 248 It
can be seen that the function of having is similar to where.
Conclusion:
1. The WHERE clause is used to filter the rows generated by the operations specified in the FROM clause.
2. The GROUP BY clause is used to group the output of the WHERE clause.
3. The HAVING clause is used to filter rows from the grouped results.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326455768&siteId=291194637