Question №8
Remaining:
Explain the difference between WHERE and HAVING
Sample Answer
Show Answer by Default
WHERE
- Filters rows before data is grouped.
- Cannot use aggregate functions (SUM(), COUNT(), AVG(), etc.).
- Applies to individual records in the table.
Example:
MySQL 8.1SELECT department_id, COUNT(*) FROM employees WHERE salary > 50000 GROUP BY department_id;
HAVING
- Filters groups of rows after the data is grouped.
- Can use aggregate functions.
- Applies to the results of GROUP BY.
Example:
MySQL 8.1SELECT department_id, COUNT(*) AS num_employees FROM employees GROUP BY department_id HAVING COUNT(*) > 5;