SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. HAVING clause

Text copied!

« Previous
Next »

HAVING clause

May 5, 2023, 4:43 p.m. under SQL

  • Hi! You should check GROUP BY clause post first.

    SQL clause is the specific part of a SQL statement which is used to perform various operations. It can be combined to create more complex queries to retrieve and manipulate data.

    HAVING clause :

    HAVING clause is always used with "GROUP BY" clause to filter the data that has already been grouped by the "GROUP BY" clause.

    It is similar to the "WHERE" clause and used to filter the results of aggregate functions. While "WHERE" clause cannot be used with aggregate functions.

    It is often used with other clauses such as 'SELECT', 'FROM' and 'ORDER BY' etc. to retrieve, specify table, sort and manipulate data.

    The syntax of the HAVING clause generally looks like this :
    SELECT column_name(s)
    FROM table_name
    GROUP BY column_name(s)
    HAVING conditions;
            

    • Specify the column(s) name after the 'SELECT' keyword.

    • Specify Asterisk (*) symbol to selects all columns from the table after the 'SELECT' keyword.

    • Specify the table name after the 'FROM' keyword.

    • Specify the column name(s) that you want to group and summarize after the 'GROUP BY' keyword.

    • Specify the conditions after the 'HAVING' keyword.

    Here's an example of how you might use the HAVING clause :

    1. Let's assume we have a table named "[Employees]".

    HAVING clause

    2. Let's assume you want to determine the number of employees by gender to know how many employees are male or female in the [Employees] table and then filter the data to identify where the count of males or females employees is equal to 2.

    3. Run below SQL statement :

    SELECT Gender, COUNT(Gender) AS [Gender_Count]
    FROM [Employees]
    GROUP BY Gender
    HAVING COUNT(Gender) = 2;
            

    4. In above statement, we used 'COUNT' function that returns the number of rows. Here, we specified the 'Gender' column in COUNT function.

    5. 'Gender column' is specified in both of the SELECT and GROUP BY clauses that group and summarize the data by gender and 'COUNT' function returns the count of 'Male' and 'Female' employees separately. We filter the data by specifying a condition in the HAVING clause, only keeping records where the count is exactly 2.

    HAVING clause

    That's it! You have successfully retrieved the number of employees by gender and then filtered it from [Employees] table.

    Frequently Asked Questions :

    What is the HAVING clause?

    The HAVING clause is used in SQL queries to filter results based on aggregate functions applied to groups.

    When would you use a HAVING clause?

    You would use a HAVING clause when you need to filter groups of rows based on aggregate conditions.

    WHERE can we use HAVING?

    The HAVING clause is used after the GROUP BY clause, while WHERE is used before.

    What is the difference between HAVING clause and a WHERE clause?

    The WHERE clause filters individual rows, whereas the HAVING clause filters groups of rows based on aggregate values.
    Thank You! You should check ORDER BY clause post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy