N049-E3 Tier 4 · Advanced · easy hr · Helix Systems

Return every department ID, the total number of employees assigned to it, and the count of employees currently active

Part of FILTER Clause on Aggregates in SQL

The problem

Helix Systems' HR director needs a headcount summary by department, split between total staff and currently active employees.

Write a query to return every department ID, the total number of employees assigned to it, and the count of employees currently active.

Assumptions:

  • The employees table has one row per employee with a department_id and an is_active flag.
  • Each department_id with at least one employee should appear once.
  • For each department, the total count covers every employee linked to that department_id. The active count covers only employees whose is_active is TRUE.

Output:

  • One row per department, with columns department_id, total_employees, and active_employees.
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(*) AS total_employees,
  COUNT(*) FILTER (
    WHERE
      is_active = TRUE
  ) AS active_employees
FROM
  employees
GROUP BY
  department_id

The shape

A boolean column is already a condition; FILTER (WHERE is_active = true) just hands that condition straight to the aggregate. Within each department_id partition, the unfiltered COUNT(*) covers every employee and the filtered count covers only the active ones.

Clause by clause

  • SELECT department_id, COUNT(*) AS total_employees, COUNT(*) FILTER (WHERE is_active = true) AS active_employees returns the department, the headcount across every employee, and the active subset of that headcount. The FILTER restricts the second COUNT to rows whose is_active is TRUE; rows where is_active is FALSE stay in the partition for the unfiltered total but drop out of the filtered count.
  • FROM employees reads the employee records.
  • GROUP BY department_id partitions the rows into per-department groups. The two counts evaluate inside each group, with department 1's 17 total and 16 active reflecting a single inactive employee in that department.

Why this and not COUNT(CASE WHEN is_active = true THEN 1 END)

For a boolean column, the CASE WHEN form is workable and produces the same numbers. FILTER is the cleaner expression of the same intent: the condition appears once, adjacent to the function it restricts, instead of buried inside an argument. The parallel between the unfiltered total and the conditional count is also more visible — both are COUNT(*) on the same partition, with only the FILTER distinguishing them.

You practiced COUNT(*) FILTER (WHERE flag = TRUE) — the cleaner shape for 'count records where a boolean column is true' alongside a total count, in a single pass.

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.