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.

Read Also:  How to Get Yii last inserted ID from database

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.

Read Also:  sql having clause Examples

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  MySQL Remove special characters from database
  2. Read Also:  sql full join Example
  3. Read Also:  Axios Post Request in vue.js
  4. Read Also:  PHP number_format() Function
  5. Read Also:  sql left outer join
  6. Read Also:  PHP Yii Maintenance Mode Tutorial with Examples
  7. Read Also:  how to store json data in mysql using php?
  8. Read Also:  Group by and Having Clause in SQL
  9. Read Also:  SQL LEFT OUTER JOIN overview and examples
  10. Read Also:  Math function in php
CLOSEX