Skip to main content
SQL • LESSON 112

Conditional Aggregation

How can we count employees by condition?

Advanced3 Minutes1250 XP
🤔 THE QUESTION

How can we count employees by condition?

💡 WHAT IS IT?

Nesting CASE inside COUNT() computes conditional counts in a single table scan without subqueries.

🎯 WHAT IS IT USED FOR?

Pivot-style metric reporting, active vs inactive counts, department breakdown columns.

💻 EXAMPLE
SELECT
COUNT(CASE WHEN department = 'Engineering' THEN 1 END) AS engineers,
COUNT(CASE WHEN department = 'Analytics' THEN 1 END) AS analysts
FROM employees;

🎯 Mission Objectives

Practice typing production-grade SQL code for Conditional Aggregation.

  • COUNT(CASE ...)
  • Conditional aggregation
  • Single-pass pivoting
  • Categorical counters