SQL Tutorial

Difference Between WHERE and HAVING in SQL

WHERE and HAVING in SQL
WHERE and HAVING in SQL

WHERE and HAVING in SQL

Filtering data is an important part of writing SQL queries, and two commonly used clauses for this purpose are WHERE and HAVING. Although both are used to filter data based on conditions, they work at different stages of query processing.

The WHERE clause filters individual rows before grouping takes place, whereas the HAVING clause filters groups after an aggregate function has been applied. Understanding this difference is also a common topic in SQL interviews.

Before looking at their differences, let’s understand each clause separately.

Difference Between WHERE and HAVING in SQL

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

WHERE Clause

The WHERE clause is used with SELECT, INSERT, UPDATE, and DELETE queries to filter records according to a specified condition.

It returns only the rows that satisfy the condition before any grouping occurs. The clause can be combined with logical operators such as AND, OR, and NOT, along with comparison operators such as <, <=, >, >=, =, and <>.

Syntax of WHERE Clause

SELECT column_list
FROM table_name
WHERE condition
GROUP BY column_list;

Example of WHERE Clause

Consider an employees table containing the following records:

emp_idemp_nameworking_hours
101Arjun10
102Meera8
103Rohan12
104Simran7

To retrieve employees who have worked for more than 9 hours, use the following query:

SELECT * FROM employees
WHERE working_hours > 9;

Output:

emp_idemp_nameworking_hours
101Arjun10
103Rohan12

HAVING Clause

The HAVING clause is generally used together with GROUP BY to filter grouped records based on aggregate functions.

Unlike WHERE, which works on individual rows, HAVING filters the groups after aggregation has taken place.

Syntax of HAVING Clause

SELECT column_list,
aggregate_function(expression)
FROM table_name
WHERE condition
GROUP BY column_list
HAVING condition;

Example of HAVING Clause

Using the same employees table, suppose we want to find employees whose total working hours exceed 15 hours across multiple days.

SELECT emp_name, SUM(working_hours) AS total_hours
FROM employees
GROUP BY emp_name
HAVING SUM(working_hours) > 15;

Output:

emp_nametotal_hours
Arjun18
Rohan20

Key Differences Between WHERE and HAVING

FeatureWHERE ClauseHAVING Clause
FunctionalityFilters individual rows before grouping.Filters groups after aggregation.
Use with Aggregate FunctionsCannot be used with aggregate functions.Can be used with aggregate functions such as SUM and COUNT.
Execution OrderApplied before GROUP BY.Applied after GROUP BY.
EfficiencyFaster because it works on raw data.Slower because it works on grouped data.
UsageCan be used with SELECT, UPDATE, and DELETE statements.Used only with SELECT statements.

Combining WHERE and HAVING

Both WHERE and HAVING can be used in the same SQL query. In this situation, WHERE first filters the raw records, and after the data is grouped, HAVING filters the resulting groups.

Example

SELECT emp_name, SUM(working_hours) AS total_hours
FROM employees
WHERE working_hours > 6
GROUP BY emp_name
HAVING SUM(working_hours) > 15;

Download New Real Time Projects :- Click here

Conclusion

Both WHERE and HAVING are used to filter information in SQL, but they work at different stages of query execution.

WHERE filters individual rows before grouping, while HAVING filters grouped results after aggregation. Understanding this distinction is essential for writing efficient SQL queries and performing accurate data analysis.


SEO Keywords

difference between where and having clause with example, difference between where and having clause class 12, difference between where and having clause w3schools, difference between having and group by, where and having in sql in same query, where and having in sql performance, where and having in sql, difference between where and having in sql, when to use where and having in sql, difference between where and having in sql server, where and having in sql query, aggregate functions in sql, sql interview questions, difference between where and having in sql with example, difference between where and having in sql server, difference between where and having in sql oracle

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

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

Chat with us