Correct option is C
In SQL, the HAVING clause is
used to filter grouped data after the GROUP BY operation. It is similar to the WHERE clause but applies to
aggregated results rather than individual rows.
Important Key Points:
1.
Difference Between WHERE and HAVING:
·
WHERE is used to filter rows before aggregation (works on individual records).
·
HAVING is used to filter groups after aggregation (works on grouped records).
2.
Correct Example of HAVING:
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
· This query first
groups employees by department, calculates the
average salary, and then filters out departments where the average salary is
greater than 50000 using HAVING.
2.
Why Option (c) is Correct?
· HAVING filters
groups rather than
individual rows.
· WHERE filters
rows before grouping, while HAVING filters
groups after aggregation.
Knowledge Booster:
·
WHERE filters before GROUP BY, HAVING filters after GROUP BY.
·
HAVING is used with aggregate functions (COUNT(), SUM(), AVG(), etc.), while WHERE works with individual records.
·
HAVING can be combined with WHERE:
SELECT department, COUNT(*)
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING COUNT(*) > 10;
· Here, WHERE filters rows with salary > 30,000 before grouping.
· HAVING filters groups where employee count > 10 after aggregation.
·
(a) Similar to the WHERE clause but is used for columns rather than groups → ❌ Incorrect because HAVING is used for
groups, not columns.
·
(b) Similar to WHERE clause but is used for rows rather than columns → ❌ Incorrect because HAVING applies to
groups, not individual rows.