Posted inMysql / Mysqli / php

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype