Get Started: SQL HAVING Clause

In This Article, You Will Know About SQL HAVING Clause

SQL HAVING Clause – Before moving ahead, let’s know a bit about GROUP BY Statement.

Table of Contents

SQL HAVING Clause

The SQL HAVING Clause is used to aggregate functions.

Syntax

				
					SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
				
			

Example Database

Table name: Data_1

S.No

Name

Class

1

Alex

5

2

Rolex

6

2

Salex

7

3

Jalex

8

3

Kalex

9

Example: Use the HAVING Clause to match the condition and return query.

				
					SELECT COUNT(Class), Name
FROM Data_1
GROUP BY Name
HAVING COUNT(Class) > 6;
				
			

As a result, it returns only the names that are having class > 6.

Example: Use the HAVING Clause to match the condition and return query.

				
					SELECT COUNT(Class), Name
FROM Data_1
GROUP BY Name
HAVING COUNT(Class) > 6;
ORDER BY COUNT(Class) ASCE;
				
			

Example Database

Table name: Data_1

S.No

Name

Class

1

Alex

5

2

Rolex

6

3

Salex

7

4

Jalex

8

5

Kalex

9

Table name: Data_2

S.No

Roll_No

Section_ID

6

451

A

7

452

B

8

453

C

9

454

D

10

455

E

Additional HAVING Examples

Example: Use the HAVING Clause to match the condition and return query.

				
					SELECT Data_1.Name, COUNT(Data_2.Roll_No) AS S.No
FROM Data_1
INNER JOIN Data_1 ON Data_1.S.No = Data_2
GROUP BY Name
ORDER BY COUNT(Data_2.Roll_No) > 453;
				
			

Example: Use the HAVING Clause to match the condition and return query.

				
					SELECT Data_1.Name, COUNT(Data_2.Roll_No) AS S.No
FROM Data_1
INNER JOIN Data_1 ON Data_1.S.No = Data_2
WHERE Name = ‘Rolex’
GROUP BY Name
ORDER BY COUNT(Data_2.Roll_No) > 451;
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

1 thought on “Get Started: SQL HAVING Clause”

Leave a Comment

Stay in the loop

codingstreets