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.

Read Also:  sql multiple subqueries in (select statement)

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):

Read Also:  sql join multiple tables with conditions

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.

Read Also:  How to update multiple rows of multiple column in sql?

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to change date format in PHP?
  2. Read Also:  Laravel Check if Object is empty Examples
  3. Read Also:  PHP Format File Size Convert KB MB GB
  4. Read Also:  PHP Converting an array to JSON Tutorial With Example
  5. Read Also:  Laravel Send Multiple Emails from on process
  6. Read Also:  SQL multiple joins for beginners with examples
  7. Read Also:  sql join multiple tables with conditions
  8. Read Also:  PHP Codeigniter 3 Multiple WHERE conditions Example
  9. Read Also:  VueJS CRUD Operations with PHP and MySQLi Example
  10. Read Also:  Update Data in MySQL Using PHP
CLOSEX