Interview Question

Difference between WHERE and HAVING?

WHERE filters input rows; HAVING filters grouped results.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

WHERE and HAVING filter at different stages of a grouped query. • WHERE removes rows before GROUP BY. • HAVING removes groups after grouping. • Use WHERE for row conditions so unnecessary rows are excluded early.

💡 SQL Example

SELECT department_id, COUNT(*) FROM employees WHERE active = true GROUP BY department_id HAVING COUNT(*) > 2;

Result

10 | 3

⚡ Quick Revision

WHERE filters input rows; HAVING filters grouped results.