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:  sql joins with examples

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:  Laravel 7 Custom Pagination View Example Tutorial

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  sql left outer join
  2. Read Also:  joins in sql
  3. Read Also:  PHP OOP CRUD MVC Framework Tutorial
  4. Read Also:  select query in php mysql with example
  5. Read Also:  sql join multiple tables with conditions
  6. Read Also:  SQL multiple joins for beginners with examples
  7. Read Also:  Get Laravel Route Parameters
  8. Read Also:  Create RESTful API using PHP
  9. Read Also:  having clause sql Example
  10. Read Also:  PHP String Concatenation Example with demo