MySQL HAVING Clause: A Complete Guide
What is the HAVING Clause in MySQL?
The HAVING
clause in MySQL is used to filter the results of a GROUP BY
query based on aggregate functions. Unlike the WHERE
clause, which filters rows before grouping, the HAVING
clause filters groups after they are created.
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name
HAVING condition;
- column_name: The column(s) to group by.
- aggregate_function: Functions like
SUM()
,COUNT()
,AVG()
,MAX()
,MIN()
. - condition: The condition to filter groups.
How HAVING Works
- Groups Data: Groups rows with the same values in specified columns.
- Applies Aggregate Functions: Calculates the aggregate values for each group.
- Filters Groups: Filters groups based on the specified condition in the
HAVING
clause.
Examples
1. Basic HAVING Example
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING employee_count > 10;
- Groups employees by department and filters departments with more than 10 employees.
Output:
Department | Employee Count |
---|---|
HR | 15 |
IT | 20 |
2. HAVING with Aggregate Functions
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
- Filters departments with an average salary greater than 50,000.
3. HAVING with Multiple Conditions
SELECT department, SUM(sales) AS total_sales
FROM employees
GROUP BY department
HAVING SUM(sales) > 100000 AND COUNT(*) > 5;
- Filters active employees using
WHERE
and then filters groups with more than 10 employees usingHAVING
.
5. HAVING Without GROUP BY
You can use HAVING
without GROUP BY
to filter based on aggregate functions applied to the entire result set.
SELECT COUNT(*) AS total_employees
FROM employees
HAVING total_employees > 50;
- Filters the total count of employees if it exceeds 50.
Best Practices
Use WHERE for Row Filtering:
Use WHERE for Row Filtering: Apply the WHERE
clause to filter rows before grouping for better performance.
Use HAVING for Aggregates
Use HAVING for Aggregates: Use HAVING
only when filtering results based on aggregate functions.
Optimize with Indexes
Optimize with Indexes: Index columns used in the WHERE
clause to speed up queries.
Avoid Overusing HAVING
Avoid Overusing HAVING: For non-aggregated filters, prefer WHERE
as it processes data more efficiently.
Differences Between WHERE and HAVING
Feature | WHERE | HAVING |
---|---|---|
When Applied | Before grouping rows. | After grouping rows. |
Use with Aggregates | Cannot be used with aggregate functions. | Can be used with aggregate functions. |
Example | WHERE age > 25 | HAVING COUNT(*) > 10 |
MySQL Workbench Instructions
- Open MySQL Workbench and connect to your database.
- Write your
HAVING
query in the SQL editor. - Execute the query to view the filtered results.
- Adjust the conditions in the
HAVING
clause as needed.
Common Errors and Troubleshooting
Error: Unknown Column in HAVING
- Ensure the column in the
HAVING
clause is either aggregated or listed in theGROUP BY
clause.
- Ensure the column in the
Incorrect Use of WHERE for Aggregates
- Use
HAVING
instead ofWHERE
for filtering aggregate values.
- Use
Performance Issues
- Optimize the query by using
WHERE
for pre-aggregate filtering and indexing relevant columns.
- Optimize the query by using
MYSQL Having
Quiz-summary
0 of 4 questions completed
Questions:
- 1
- 2
- 3
- 4
Information
Quiz: Test Your Knowledge
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 4 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- Answered
- Review
- Question 1 of 4
1. Question
What does the HAVING clause do?
CorrectIncorrect - Question 2 of 4
2. Question
Which query filters departments with total sales over 100,000?
CorrectIncorrect - Question 3 of 4
3. Question
Which clause is used to filter aggregate data?
CorrectIncorrect - Question 4 of 4
4. Question
What is the output of this query?
“`sql
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING employee_count > 5;
“`CorrectIncorrect