Count Employees Per Department

SQL coding challenge · Difficulty: easy · Topic: Joins · +50 XP

Problem

The company wants an org chart overview. Show every department and how many employees it currently has. Departments with no staff must still appear.

Tables

Table: Employee

| employee_id | name | department_id |
| --- | --- | --- |
| 1 | Krishna | 1 |
| 2 | Parvati | 2 |
| 3 | Ashna | 3 |
| 4 | Pinki | 1 |
| 5 | Shruti | 4 |

Table: Department

| department_id | department_name |
| --- | --- |
| 1 | HR |
| 2 | Finance |
| 3 | IT |
| 4 | Sales |

Expected Output

| department_name | employee_count |
| --- | --- |
| Finance | 1 |
| HR | 2 |
| IT | 1 |
| Sales | 1 |

Success Criteria

Production Constraints

Why This Matters

Reports built with an INNER JOIN silently drop groups that have no matching rows -- a department with no staff would simply vanish from an org chart instead of showing zero. Pairing a LEFT JOIN with COUNT is the standard pattern for producing a complete, warehouse-ready aggregate.

What this SQL challenge teaches you

“Count Employees Per Department” is a easy-level SQL challenge focused on Joins. Working through it gives you hands-on practice with scenario:production_scenario, LEFT JOIN, COUNT, GROUP BY — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Counting per department means grouping by the department, not by the employee.
  2. Join the two tables, then GROUP BY the department and COUNT the employees.
  3. Which join you use decides what happens to a department with nobody in it -- check the Expected Output to see whether it should appear.

Where this comes up

Variations of this problem have been reported in interviews at Microsoft, TCS, Infosys. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated easy and covers Joins.

Solve this challenge free on PySpark.in