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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.