DAT - BLOG POST 7
- daniel-ewers2
- Jun 20, 2021
- 2 min read
Sessions 11-12 – Group by Having
This week I learned about ‘group by having’ statements. These statements are used instead of WHERE statements in the situations where we are using aggregate functions as WHERE statements cannot be used for them. In saying this, we can use a WHERE statement in the same query as a HAVING statement. A WHERE statement is applied to individual records whereas a HAVING statement applies to summarized group records. Only records that have met the HAVING criteria will be returned, like that of a WHERE statement. A GROUP BY clause must be present in order to employ a HAVING statement.
Here is an example of a ‘group by having’ statement:

This only selects countries with more than 5 cities.
Why Have I Learned This?
I believe that I have learned this skill as it can be useful in scenarios where I might want to summarize data from a database. Rather than calculating a table count application side, we can perform it database side and return the value that we want. For example, we may want to get the count of males and females having an age above 33. This can return a numerical value of the male count and a numerical value of the female count, as seen below:
| Gender | COUNT(‘Gender) |
| Female | 132 |
| Male | 222 |
How Have I Learned This?
I have learned this by doing some research into what ‘group by having’ statements are and how I implement them into my code. I did some tests on the tables that I have created for my game to see if it works as expected. I got the expected results and am satisfied that I understand how to use them for future tasks.



Comments