How to use having in SQL
How to use having in SQL? In view of this problem, today the editor summarizes this article about having, hoping to help more friends who want to solve this problem to find a more simple and feasible way.
"having" is a statistical filter after "group by". Generally, "having" is used with "group by". When you use it, you need to group it first by "group by", and then carry out "having" statistical filtering, such as judging whether the value of the aggregate function is greater than a certain value.
SQL instance
First, display the total population and total area of each area.
SELECT region, SUM (population), SUM (area) FROM bbc GROUP BY region
First, the returned records are divided into groups with region, which is the literal meaning of GROUP BY. After the group is divided into groups, then use the aggregate function to compare each group
Perform operations on different fields (one or more records) of
Second, display the total population and total area of each region. Only those areas with an area over 1000000 are shown.
SELECT region, SUM (population), SUM (area) FROM bbcGROUP BY regionHAVING SUM (area) > 1000000
Here, we cannot use where to filter more than 1000000 regions, because such a record does not exist in the table.
On the contrary, the having clause allows us to filter groups of data
Mysql determines the length of a field:
Select home_page from aaa table where char_length (trim (home_page)) 1
So much for sharing the usage of having. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.