Junior — Middle
What methods exist for filtering data without using the HAVING clause in SQL?
sobes.tech AI
Answer from AI
To filter data without using the HAVING clause in SQL, you can apply the following methods:
- Use WHERE to filter rows before grouping. For example, filter by values of individual records.
- Use subqueries where aggregated data is selected first, and then a filter is applied in the main query.
- Use window functions (if supported by the DBMS) to filter data based on aggregates without HAVING.
Example with a subquery:
SELECT department, total_salary
FROM (
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
) AS dept_salaries
WHERE total_salary > 100000;
Here, filtering by total salaries occurs in the outer query without HAVING.