N027-E3 Tier 2 · Core SQL · easy hr · Helix Systems

Return the department ID, the active count, and the inactive count

Part of Conditional Aggregation (CASE inside Aggregates) in SQL

The problem

Helix Systems' HR team needs a headcount breakdown for each department: how many employees are currently active and how many are inactive.

Write a query to return the department ID, the active count, and the inactive count.

Assumptions:

  • The employees table contains every active and former employee.
  • Active employees have is_active = TRUE; inactive employees have is_active = FALSE.
  • Both counts are produced from the same per-department group.

Output:

  • One row per department, with columns department_id, active_count, and inactive_count.
Schema · hr 4 tables
departments
id integer
name text
location text
budget numeric
salaries
id integer
employee_id integer
amount numeric
effective_date date
end_date? date
employees
id integer
name text
email text
department_id integer
manager_id? integer
hire_date date
title text
is_active boolean
job_history
id integer
employee_id integer
title text
department_id integer
start_date date
end_date? date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  department_id,
  COUNT(
    CASE
      WHEN is_active = TRUE THEN 1
    END
  ) AS active_count,
  COUNT(
    CASE
      WHEN is_active = FALSE THEN 1
    END
  ) AS inactive_count
FROM
  employees
GROUP BY
  department_id

The shape

The boolean column is_active carries the active/inactive distinction directly — no string comparison needed. Two COUNT(CASE WHEN ... THEN 1 END) expressions split each department's employees into active and inactive headcounts in one per-department pass. Department 1 has 16 active and 1 inactive; department 5 has 4 active and 0 inactive.

Clause by clause

  • department_id is the grouping column, present in both the SELECT list and the GROUP BY clause per the N014 rule.
  • COUNT(CASE WHEN is_active = TRUE THEN 1 END) AS active_count returns 1 for TRUE rows and NULL for FALSE rows. COUNT tallies the non-NULL values inside each department's group.
  • COUNT(CASE WHEN is_active = FALSE THEN 1 END) AS inactive_count is the mirror image. The same row that contributed 1 to the active count contributes NULL here.
  • FROM employees GROUP BY department_id partitions the employees per department; the two aggregates evaluate independently inside each partition.

Why this and not is_active alone in the predicate

PostgreSQL accepts a boolean expression directly inside CASE WHEN, so WHEN is_active THEN 1 would work and return the same result. Writing is_active = TRUE is the more explicit form, and it parallels the is_active = FALSE branch one line down. When the two predicates sit side by side, the symmetry makes the intent easier to read at a glance.

You practiced conditional counting against a boolean column. The shape works the same as against a string column — only the predicate inside CASE changes; the surrounding COUNT(CASE WHEN ... THEN 1 END) scaffold is identical.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.