Count employees with status 'ACTIVE' in every department; departments with no active employees must still appear with 0. Employees without a department are ignored. Return dept_name and active_headcount. Order by active_headcount DESC, then dept_name.
| Column | Type |
|---|---|
| dept_id | INTEGER |
| dept_name | TEXT |
| dept_id | dept_name |
|---|---|
| 1 | Cloud |
| 2 | Data & AI |
| 3 | Security |
| 4 | Finance Ops |
| 5 | Consulting |
| Column | Type |
|---|---|
| emp_id | INTEGER |
| emp_name | TEXT |
| dept_id | INTEGER |
| status | TEXT |
| emp_id | emp_name | dept_id | status |
|---|---|---|---|
| 1 | Aarav | 1 | ACTIVE |
| 2 | Bela | 1 | ACTIVE |
| 3 | Chirag | 2 | ACTIVE |
| 4 | Dia | 2 | EXITED |
| 5 | Ehsaan | 1 | EXITED |
| 6 | Farah | 5 | ACTIVE |
| dept_name | active_headcount |
|---|---|
| Cloud | 2 |
| Consulting | 1 |
| Data & AI | 1 |
| Finance Ops | 0 |
| Security | 0 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Unbalanced Journal Entries · Billable Hours per Client in 2025 ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.