sajad torkamani

In a nutshell

The HAVING clause in MySQL is used to filter the aggregated results of a GROUP BY operation. It is similar to the WHERE clause, but while the WHERE cause filters rows before they’re grouped and aggregated, the HAVING cause filters the aggregated results.

Example

Suppose you had the following orders table:

customer_idorder_amount
11000
12000
24000

You could use the following query to find all customers and the sum of their orders:

select 
  customer_id,
  sum(order_amount) as total
from
  orders
group by
  customer_id

You can then use HAVING to filter the aggregated results:

select 
  customer_id,
  sum(order_amount) as total
from
  orders
group by
  customer_id
having
  total > 3000
Tagged: MySQL

Leave a comment

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