Home » SQL

SQL - HAVING Clause

SQL - HAVING Clause: Here, we will learn how to use conditions on the groups formed by GROUP BY?
Submitted by Shubham Singh Rajawat, on November 15, 2017

Prerequisite: SQL - GROUP BY

Having Clause is used to set conditions on the groups formed from GROUP BY, it also uses aggregate functions.

Syntax:

SELECT column1, column2, ...  FROM table_name 
WHERE condition 
GROUP BY column1, column2, ... 
HAVING condition;

Here,

  • Column_names: are the name of the fields in the table
  • table_name: name of the table from which we want to fetch data
  • condition: use to filter data

Following are the functions that can be used with having clause

  • COUNT - counts number of rows in a table
  • AVG - calculate the average of the given values
  • MAX - returns maximum of the given values
  • MIN - returns minimum of the given values
  • SUM - calculates the sum of the given values
  • STDDEV - calculates standard deviation

Sample table (Student),

student table in sql example

Example:

1) Count number of cities

SELECT * FROM Student 
GROUP BY City 
HAVING COUNT(City) >1;
HAVING CLAUSE SQL example

2) To show students whose marks are greater than 89 in physics?

SELECT * FROM Student 
GROUP BY City 
HAVING MAX(physics)>89;
HAVING MAX SQL example




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.