Posted inphp / Mysql / Mysqli

Group by and Having Clause in SQL

Today, We want to share with you sql having.In this post we will show you sql having vs where, hear for sql subquery we will give you demo and example for implement.In this post, we will learn about subquery in sql with an example.

SQL HAVING and GROUP BY – with Examples

In this tutorial we learn to all about having Examples like as a HAVINGs Clause, GROUP BY, ORDER BY COUNT, or many more.

Also Read: sql subquery examples

Query clause order

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

HAVING Clause

Syntax

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

SQL HAVING Examples

SELECT COUNT(MemberID), AddressCode
FROM Members
GROUP BY AddressCode
HAVING COUNT(MemberID) > 5;

The bellow SQL statement lists the number of members in each AddressCode, sorted high to low (Only include AddressCodes with more than 5 members):

SELECT COUNT(MemberID), AddressCode
FROM Members
GROUP BY AddressCode
HAVING COUNT(MemberID) > 5
ORDER BY COUNT(MemberID) DESC;

Example – 1

The bellow SQL statement lists the members that have registered more than 10 orders:

SELECT Members_lists.ProfileNm, COUNT(Request.ReqID) AS NumberOfRequest
FROM (Request
INNER JOIN Members_lists ON Request.Members_listID = Members_lists.Members_listID)
GROUP BY ProfileNm
HAVING COUNT(Request.ReqID) > 10;

Example – 2

The bellow SQL statement lists if the members “Bhakti” or “Rupareliya” have registered more than 25 orders:

SELECT Members_lists.ProfileNm, COUNT(Request.ReqID) AS NumberOfRequest
FROM Request
INNER JOIN Members_lists ON Request.Members_listID = Members_lists.Members_listID
WHERE ProfileNm = 'Bhakti' OR ProfileNm = 'Rupareliya'
GROUP BY ProfileNm
HAVING COUNT(Request.ReqID) > 25;

I hope you get an idea about sql having.
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