SQL Interview Questions
SQL
BackendWeb DevelopmentData ScienceQuestion 7
What is the HAVING clause, and how is it different from WHERE?
Answer:
The HAVING clause in SQL is used to filter the results of a query that includes a GROUP BY clause. It is similar to the WHERE clause but is applied after the aggregation has been performed. The HAVING clause allows you to filter groups of data, whereas the WHERE clause filters rows of data before any groupings are made.
Key Differences between WHERE and HAVING
- 
Purpose:
WHEREClause: Filters rows before any grouping or aggregation is performed. It is used to set conditions on individual rows.HAVINGClause: Filters groups after the aggregation is performed. It is used to set conditions on aggregated data.
 - 
Usage Context:
WHEREClause: Used withSELECT,UPDATE,DELETE, etc., to filter rows.HAVINGClause: Used only withSELECTstatements that includeGROUP BYto filter the aggregated results.
 - 
Filter Scope:
WHEREClause: Cannot use aggregate functions directly.HAVINGClause: Can use aggregate functions directly to filter groups based on aggregated values.
 
Examples
Suppose we have a table sales with the following structure:
sales
+----+---------+--------+-------+
| id | product | amount | year  |
+----+---------+--------+-------+
| 1  | A       | 100    | 2023  |
| 2  | B       | 150    | 2023  |
| 3  | A       | 200    | 2024  |
| 4  | B       | 100    | 2024  |
| 5  | C       | 50     | 2023  |
+----+---------+--------+-------+
Using WHERE Clause
To filter rows where the amount is greater than 100:
SELECT product, amount, year
FROM sales
WHERE amount > 100;
Result:
+---------+--------+-------+
| product | amount | year  |
+---------+--------+-------+
| B       | 150    | 2023  |
| A       | 200    | 2024  |
+---------+--------+-------+
Using HAVING Clause
To filter products with total sales greater than 150:
SELECT product, SUM(amount) AS total_sales
FROM sales
GROUP BY product
HAVING SUM(amount) > 150;
Result:
+---------+-------------+
| product | total_sales |
+---------+-------------+
| A       | 300         |
| B       | 250         |
+---------+-------------+
Combined Usage of WHERE and HAVING
To filter rows first where the year is 2023, and then filter groups where total sales are greater than 100:
SELECT product, SUM(amount) AS total_sales
FROM sales
WHERE year = 2023
GROUP BY product
HAVING SUM(amount) > 100;
Result:
+---------+-------------+
| product | total_sales |
+---------+-------------+
| B       | 150         |
+---------+-------------+
Summary
- 
WHEREClause:- Filters individual rows before aggregation.
 - Used with conditions on columns directly.
 - Cannot use aggregate functions directly.
 
 - 
HAVINGClause:- Filters groups after aggregation.
 - Used with conditions on aggregate functions.
 - Requires a 
GROUP BYclause to be meaningful. 
 
Understanding the distinction between WHERE and HAVING is crucial for writing efficient and accurate SQL queries, especially when working with aggregated data.