Contents
SQL HAVING Clause: Main Tips
- The HAVING clause was added to SQL to add more functionality.
- WHERE keyword can not be used in union with other functions. That where HAVING clause come in handy.
- HAVING clause is often used with the COUNT function.
Syntax of SQL HAVING
Example
SELECT name_of_column(s)
FROM name_of_table
WHERE condition
GROUP BY name_of_column(s)
HAVING condition
ORDER BY name_of_column(s);
Pros Main Features
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
Pros Main Features
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
Pros Main Features
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Demo Database
Let us say we have a database with information about pets.
Below is a "Pets" table:
ID | City_ID | Breed_ID | Birthdate | Country |
---|---|---|---|---|
696 | 90 | 5 | 2009-08-14 | USA |
670 | 81 | 6 | 2012-09-02 | UK |
671 | 34 | 4 | 2013-01-01 | NO |
SQL HAVING: Example
If we want to get the countries that have registered, for example, more than 2 pets, we would use HAVING clause.
The example below shows how to use it.
Example
SELECT COUNT(ID), Country
FROM Pets
GROUP BY Country
HAVING COUNT(ID) > 2;