MySQL HAVING

MySQL HAVING

MySQL HAVING Clause

The HAVING clause in MySQL is used to filter the results of a GROUP BY operation based on aggregate function conditions. It is similar to the WHERE clause but specifically operates on grouped data.


Key Points

  1. The HAVING clause is used after the GROUP BY clause.
  2. It allows you to filter aggregated data, such as sums, averages, counts, etc.
  3. If no GROUP BY is present, HAVING behaves like a WHERE clause but can still use aggregate functions.

Syntax

SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1 HAVING condition;
  • column1: The column to group by.
  • aggregate_function(column2): An aggregate function like SUM, COUNT, AVG, etc.
  • condition: The filtering condition applied to the aggregated data.

Example Table

Table: sales

plaintext
+----+---------+--------+-------+ | id | product | region | sales | +----+---------+--------+-------+ | 1 | Laptop | North | 500 | | 2 | Laptop | South | 300 | | 3 | Phone | North | 200 | | 4 | Phone | South | 100 | | 5 | Tablet | North | 400 | | 6 | Tablet | South | 600 | +----+---------+--------+-------+

1. Using HAVING with GROUP BY

Find products with total sales greater than 800:

SELECT product, SUM(sales) AS total_sales FROM sales GROUP BY product HAVING total_sales > 800;

Result

+---------+-------------+ | product | total_sales | +---------+-------------+ | Tablet | 1000 | +---------+-------------+

2. HAVING Without GROUP BY

The HAVING clause can filter aggregate results even without a GROUP BY:

SELECT SUM(sales) AS total_sales FROM sales HAVING total_sales > 2000;

Result

+-------------+ | total_sales | +-------------+ | 2100 | +-------------+

3. Using HAVING with Multiple Conditions

Find products with total sales between 600 and 900:

SELECT product, SUM(sales) AS total_sales FROM sales GROUP BY product HAVING total_sales BETWEEN 600 AND 900;

Result

+---------+-------------+ | product | total_sales | +---------+-------------+ | Laptop | 800 | +---------+-------------+

4. Combining WHERE and HAVING

  • Use the WHERE clause to filter rows before grouping.
  • Use the HAVING clause to filter aggregated data.

Find total sales per region where individual sales are greater than 300, and total sales are more than 800:

SELECT region, SUM(sales) AS total_sales FROM sales WHERE sales > 300 GROUP BY region HAVING total_sales > 800;

Result

+--------+-------------+ | region | total_sales | +--------+-------------+ | North | 900 | | South | 600 | +--------+-------------+

5. Using Aggregate Functions in HAVING

Find regions with more than 1 product category having sales greater than 300:

SELECT region, COUNT(product) AS product_count FROM sales WHERE sales > 300 GROUP BY region HAVING product_count > 1;

Result

+--------+--------------+ | region | product_count| +--------+--------------+ | North | 2 | +--------+--------------+

6. HAVING with Aliases

You can use column aliases in the HAVING clause. For example:

SELECT product, SUM(sales) AS total_sales FROM sales GROUP BY product HAVING total_sales > 800;

HAVING vs. WHERE

FeatureHAVINGWHERE
PurposeFilters aggregated/grouped dataFilters rows before grouping
Used WithGROUP BYAny query
Aggregate FuncsAllowed (e.g., SUM, COUNT)Not allowed

Performance Considerations

  1. WHERE First, HAVING Later:

    • Use WHERE to filter rows before grouping to reduce the amount of data processed.
  2. Indexes:

    • Indexes are used with the WHERE clause but do not optimize the HAVING clause since it works on grouped data.

Use Cases

  • Filter summarized data (e.g., sales totals, averages).
  • Enforce conditions on grouped results.
  • Apply advanced filters combining WHERE and HAVING.

Let me know if you'd like additional examples or help crafting a query!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close