Skip to main content
SQL โ€ข LESSON 174

Conditional Aggregation

How can we count active and inactive employees per department side-by-side?

Advancedโฑ 3 Minutesโญ 1870 XP
๐Ÿค” THE QUESTION

How can we count active and inactive employees per department side-by-side?

๐Ÿ’ก WHAT IS IT?

Combining SUM(CASE WHEN ... THEN 1 ELSE 0 END) with GROUP BY pivots categorical statuses into distinct columns.

๐ŸŽฏ WHAT IS IT USED FOR?

Executive matrix dashboards, headcount status breakdowns, multi-state summaries.

๐Ÿ’ป EXAMPLE
SELECT
department_id,
SUM(CASE WHEN status = 'Active' THEN 1 ELSE 0 END) AS active_count,
SUM(CASE WHEN status = 'Inactive' THEN 1 ELSE 0 END) AS inactive_count
FROM employees
GROUP BY department_id;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Conditional Aggregation.

  • โœ“ Pivoted conditional sums
  • โœ“ Active vs Inactive matrix
  • โœ“ GROUP BY with CASE
  • โœ“ Executive status reporting