SQL GROUP BY and HAVING Clause with Examples

Today, We want to share with you having sql.In this post we will show you joins in MySQL, hear for sql subquery we will give you demo and example for implement.In this post, we will learn about MySQL Subquery, IN SELECT – with Examples with an example.

group by and having clause in sql

In this tutorial we learn to all about having Examples like as a SQL GROUP BY, having clause, AVG Function, MAX Function, MIN Function or many more.

Also Read: subquery multiple tables Example

Query clause order

  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY

The SQL HAVING syntax

SELECT column-names
  FROM table-name
 WHERE condition
 GROUP BY column-names
HAVING condition

The general syntax with ORDER BY is:

SELECT column-names
  FROM table-name
 WHERE condition
 GROUP BY column-names
HAVING condition
 ORDER BY column-names

SQL GROUP BY Examples

SELECT COUNT(Id), AddressCode 
  FROM Member
 GROUP BY AddressCode
HAVING COUNT(Id) > 10
SELECT COUNT(Id), AddressCode 
  FROM Member
 WHERE AddressCode <> 'RJK005'
 GROUP BY AddressCode
HAVING COUNT(Id) >= 9
 ORDER BY COUNT(Id) DESC

having clause sql Example

SELECT AVG(fullIncome), FullNm, ProfileNm
  FROM [Order] O JOIN Member C ON O.MemberId = C.Id
 GROUP BY FullNm, ProfileNm
HAVING AVG(fullIncome) BETWEEN 1000 AND 1200

Using AVG Function:

SELECT Section, AVG(Income) AS 'Average income'
FROM Members_list
GROUP BY Section
HAVING AVG(Income) > 27000;

Using MAX Function:

SELECT Section, MAX(Income) AS 'Highest income'
FROM Members_list
GROUP BY Section
HAVING MAX(Income) > 27000;

Using MIN Function:

SELECT Section, MIN(Income) AS 'Lowest income'
FROM Members_list
GROUP BY Section
HAVING MIN(Income) > 25000;

I hope you get an idea about having sql.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment