w3resource

SQL COUNT() with HAVING

COUNT() with HAVING

The HAVING clause with SQL COUNT() function can be used to set a condition with the select statement. The HAVING clause is used instead of WHERE clause with SQL COUNT() function.

The GROUP BY with HAVING clause retrieves the result for a specific group of a column, which matches the condition specified in the HAVING clause.

Example:

Sample table : agents
 
+------------+----------------------+--------------------+------------+-----------------+---------+
| AGENT_CODE | AGENT_NAME           | WORKING_AREA       | COMMISSION | PHONE_NO        | COUNTRY |
+------------+----------------------+--------------------+------------+-----------------+---------+
| A007       | Ramasundar           | Bangalore          |       0.15 | 077-25814763    |         |
| A003       | Alex                 | London             |       0.13 | 075-12458969    |         |
| A008       | Alford               | New York           |       0.12 | 044-25874365    |         |
| A011       | Ravi Kumar           | Bangalore          |       0.15 | 077-45625874    |         |
| A010       | Santakumar           | Chennai            |       0.14 | 007-22388644    |         |
| A012       | Lucida               | San Jose           |       0.12 | 044-52981425    |         |
| A005       | Anderson             | Brisban            |       0.13 | 045-21447739    |         |
| A001       | Subbarao             | Bangalore          |       0.14 | 077-12346674    |         |
| A002       | Mukesh               | Mumbai             |       0.11 | 029-12358964    |         |
| A006       | McDen                | London             |       0.15 | 078-22255588    |         |
| A004       | Ivan                 | Torento            |       0.15 | 008-22544166    |         |
| A009       | Benjamin             | Hampshair          |       0.11 | 008-22536178    |         |
+------------+----------------------+--------------------+------------+-----------------+---------+

To get data of number of agents from the 'agents' table with the following condition -

1. number of agents must be greater than 3,

the following SQL statement can be used:

-- Counting the total number of rows in the 'agents' table
SELECT COUNT(*)
-- From the 'agents' table
FROM agents
-- Having clause filters the results based on the aggregate condition
-- Counting the number of occurrences and including only those with a count greater than 3
HAVING COUNT(*) > 3;

Explanation:

  • SELECT COUNT(*): This is the main part of the SQL query. It selects the count of all rows from the 'agents' table using the COUNT(*) function. The result will be a single row with a single column containing the total number of rows in the 'agents' table.
  • FROM agents: This specifies the source of the data for the query, which is the 'agents' table. The FROM keyword is used to indicate the table from which the data will be selected. In this case, it selects data from the 'agents' table.
  • HAVING COUNT(*) > 3: This clause filters the groups formed by the GROUP BY clause based on an aggregate condition. The HAVING clause is similar to the WHERE clause, but it is used with aggregate functions like COUNT(). Here, it filters the groups where the count of rows in each group is greater than 3.

Relational Algebra Expression:

Relational Algebra Expression: COUNT() with HAVING.

Relational Algebra Tree:

Relational Algebra Tree: COUNT() with HAVING.

Output:

  COUNT(*)
----------
        12

Visual Presentation :

SQL COUNT with HAVING

SQL COUNT( ) with having and group by

Sample table : agents
 
+------------+----------------------+--------------------+------------+-----------------+---------+
| AGENT_CODE | AGENT_NAME           | WORKING_AREA       | COMMISSION | PHONE_NO        | COUNTRY |
+------------+----------------------+--------------------+------------+-----------------+---------+
| A007       | Ramasundar           | Bangalore          |       0.15 | 077-25814763    |         |
| A003       | Alex                 | London             |       0.13 | 075-12458969    |         |
| A008       | Alford               | New York           |       0.12 | 044-25874365    |         |
| A011       | Ravi Kumar           | Bangalore          |       0.15 | 077-45625874    |         |
| A010       | Santakumar           | Chennai            |       0.14 | 007-22388644    |         |
| A012       | Lucida               | San Jose           |       0.12 | 044-52981425    |         |
| A005       | Anderson             | Brisban            |       0.13 | 045-21447739    |         |
| A001       | Subbarao             | Bangalore          |       0.14 | 077-12346674    |         |
| A002       | Mukesh               | Mumbai             |       0.11 | 029-12358964    |         |
| A006       | McDen                | London             |       0.15 | 078-22255588    |         |
| A004       | Ivan                 | Torento            |       0.15 | 008-22544166    |         |
| A009       | Benjamin             | Hampshair          |       0.11 | 008-22536178    |         |
+------------+----------------------+--------------------+------------+-----------------+---------+

To get data of 'commission' and number of agents for that commission from the 'agents' table with the following conditions -

1. number of agents for a particular 'commisson',

2. number of agents for that particular 'commission' must be more than 3,

the following SQL statement can be used :

-- Selecting the 'commission' column and counting the number of occurrences for each distinct value
SELECT commission, COUNT(*)
-- From the 'agents' table
FROM agents
-- Grouping the results by the 'commission' column
GROUP BY commission
-- Having clause filters the results based on the aggregate condition
-- Counting the number of occurrences and including only those with a count greater than 3
HAVING COUNT(*) > 3;

Explanation:

  • SELECT commission, COUNT(*): This is the main part of the SQL query. It selects the 'commission' column from the 'agents' table and counts the number of occurrences of each distinct value in the 'commission' column using the COUNT(*) function. The result will include two columns: 'commission' and the count of occurrences.
  • FROM agents: This specifies the source of the data for the query, which is the 'agents' table. The FROM keyword is used to indicate the table from which the data will be selected. In this case, it selects data from the 'agents' table.
  • GROUP BY commission: This clause groups the result set by the 'commission' column. The GROUP BY clause is used with aggregate functions like COUNT() to divide the rows returned from the SELECT statement into groups based on the values in one or more columns. In this case, it groups the rows based on the values in the 'commission' column.
  • HAVING COUNT(*) > 3: This clause filters the groups formed by the GROUP BY clause based on an aggregate condition. The HAVING clause is similar to the WHERE clause, but it is used with aggregate functions like COUNT(). Here, it filters the groups where the count of rows in each group is greater than 3.

Relational Algebra Expression:

Relational Algebra Expression: COUNT() with having and group by.

Relational Algebra Tree:

Relational Algebra Tree: COUNT() with having and group by.

Output:

COMMISSION   COUNT(*)
---------- ----------
       .15          4

Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

Here is a slide presentation of all aggregate functions.

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: COUNT with Group by
Next: SUM function



Follow us on Facebook and Twitter for latest update.