Skip to main content
SQL โ€ข LESSON 112

Conditional Aggregation

How can we count employees by condition?

Advancedโฑ 3 Minutesโญ 1250 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