---
title: "Write a query to retrieve the total number of employees in each department."  
description: "Write a query to retrieve the total number of employees in each department."  
author: "Ravi Vishwakarma"  
published: 2024-07-03  
updated: 2024-07-03  
canonical: https://www.mindstick.com/interview/33938/write-a-query-to-retrieve-the-total-number-of-employees-in-each-department  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2012"]  
reading_time: 2 minutes  

---

# Write a query to retrieve the total number of employees in each department.

To retrieve the total number of employees in each department from an SQL Server table, you can use the `COUNT()` function along with `GROUP BY`. Here's how you can write the query:

```plaintext
SELECT Department, COUNT(EmployeeID) AS NumEmployees
FROM YourTableName
GROUP BY Department;
```

## Output-

| Department | NumEmployees |
| --- | --- |
| Human Resources (HR) | 25 |
| Finance | 5 |
| **Information Technology (IT)** | 20 |
| Marketing | 5 |
| Research and Development (R&D) | 10 |

In this query:

- **YourTableName**: Replace this with the actual name of your table that contains employee data.
- **Department**: This is the column in your table that represents each employee's department.
- **EmployeeID**: Replace this with the actual column name that uniquely identifies each employee in your table (e.g., `EmployeeID`, `EmployeeNumber`, etc.).

The `GROUP BY Department` clause groups the results by the `Department` column, and `COUNT(EmployeeID)` counts the number of employees (`EmployeeID`) in each department. This will give you the total number of employees in each department listed in your table.

## Answers

### Answer by Ravi Vishwakarma

To retrieve the total number of employees in each department from an SQL Server table, you can use the `COUNT()` function along with `GROUP BY`. Here's how you can write the query:

```plaintext
SELECT Department, COUNT(EmployeeID) AS NumEmployees
FROM YourTableName
GROUP BY Department;
```

## Output-

| Department | NumEmployees |
| --- | --- |
| Human Resources (HR) | 25 |
| Finance | 5 |
| **Information Technology (IT)** | 20 |
| Marketing | 5 |
| Research and Development (R&D) | 10 |

In this query:

- **YourTableName**: Replace this with the actual name of your table that contains employee data.
- **Department**: This is the column in your table that represents each employee's department.
- **EmployeeID**: Replace this with the actual column name that uniquely identifies each employee in your table (e.g., `EmployeeID`, `EmployeeNumber`, etc.).

The `GROUP BY Department` clause groups the results by the `Department` column, and `COUNT(EmployeeID)` counts the number of employees (`EmployeeID`) in each department. This will give you the total number of employees in each department listed in your table.


---

Original Source: https://www.mindstick.com/interview/33938/write-a-query-to-retrieve-the-total-number-of-employees-in-each-department

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
