Browse SQL Topics ▼

WHERE vs HAVING

📂 Summarizing Data 📘 Lesson 40 of 81

Master the definitive distinction between filtering individual raw rows with WHERE versus aggregated groups with HAVING.

WHERE vs HAVING is an essential topic in SQL and relational database engineering.

Overview

Building on foundational principles introduced in having, mastering where vs having provides the core mental model and practical query skills needed for production relational database engineering.

SELECT department, COUNT(*) AS active_workers
FROM employees
WHERE status = 'active'     -- 1. Filters raw rows before grouping
GROUP BY department
HAVING COUNT(*) >= 3;       -- 2. Filters groups after aggregation

Key Concepts

  • Understand the fundamental syntax, conventions, and mental model.
  • Avoid common beginner traps, data type mismatches, and performance pitfalls.
  • Apply practical idioms directly in production applications.

[!TIP] Master this concept to build a rock-solid foundation. Next, proceed to database-relationships.

Test your understanding of WHERE vs HAVING

Quick interactive practice questions with instant feedback.

Take Quiz →