alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

SQL -Structured Query Language - Sql Clause - Having Clause Tutorial

' HAVING ' clause is used to filter out the column with aggregate functions like count, sum, avg, min, max. It is done based on groups created by the GROUP BY Clause.

' WHERE ' clause can't be used to filter the column with an aggregate function. It is only used to filter normal columns.

 

Syntax-

The basic syntax of the HAVING clause with aggregate functions like count, sum, avg, min, max.

SELECT function_name(column_name),..,column_name_group 
FROM 
table_name 
GROUP BY 
column_name_group 
HAVING 
aggregate_function_condition;

Here function_name can be count, sum, avg, min, max, and column_name_group is the name of the column to be group by.

aggregate_function_condition is the condition based on aggregate function like count, sum, avg, min, max.

 

For example –

SELECT COUNT(employee_id) 
FROM 
employees 
GROUP BY department_id 
HAVING COUNT(employee_id)>5;

In this statement, it will retrieve the number of employees in each department, whose department has employees greater than 5.


SELECT MAX(salary) 
FROM 
employees 
GROUP BY department_id 
HAVING MAX(salary)>8000;

In this statement, it will find the maximum salary of the employee in each department, whose department maximum salary is greater than 8000.


SELECT MIN(salary) 
FROM 
employees 
GROUP BY department_id 
HAVING MIN(salary)>8000;

In this statement, it will Find the minimum salary of the employee in each department, whose department minimum salary is greater than 8000.


SELECT AVG(salary) 
FROM 
employees 
GROUP BY department_id 
HAVING AVG(salary)<8000;

In this statement, it will Calculate the average salary of the employee in each department. whose department average salary is less than 8000.


SELECT SUM(salary) 
FROM 
employees 
GROUP BY department_id 
HAVING SUM(salary)<20000;

In this statement, it will Calculate the sum of the salary of the employee in each department. whose department salary sum is less than 20000.

Run Query

SQL -Structured Query Language

SQL -Structured Query Language

  • Introduction
  • Overview
    • How To Download And Install MySql Workbench
    • SQL Syntax
  • SQL Database
    • Create Database
    • Drop Database
    • Use / Select Database
  • SQL Table
    • Create Table
    • Alter - Rename Table
    • Alter - Add Column
    • Alter - Modify Column
    • Alter - Rename Column
    • Alter - Drop Column
    • Copy Table
    • Drop Table
  • SQL Insert
    • Insert Record
  • Sql Update
    • Update Record
  • Sql Select
    • Select Record
    • Select - Count
    • Select - Average
    • Select - Sum
    • Select - Min Max
    • Select - Distinct
    • Select - Limit
    • Select - First
    • Select - Last
  • Sql Clause
    • Like Clause
    • And Operator
    • Or Operator
    • Between & Not Between Operator
    • As or Alias
    • In Clause
    • Group By Clause
    • Having Clause
  • Sql Order By
    • Ascending
    • Descending
    • Random
    • Ascending Descending Multiple Column
  • SQL Joins
    • Joins and its type
    • Inner Join
    • Left Join
    • Right Join
    • Full Join
    • Cross Join
  • SQL View
    • Create View
    • Update View
    • Delete View
  • SQL Delete
    • Delete Table
    • Truncate Table
  • Sql Key & Constraint
    • Primary Key
    • Foreign Key
    • Unique Key
    • Not Null Constraint
    • Check Constraint
    • Default Constraint
  • Normalization
    • Overview
    • 1 NF
    • 2 NF
    • 3 NF
    • Boyce Codd normal form ( BCNF )

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.