Displaying Department Name Having Least Number of Employees in SQL Server
Last Updated :
25 May, 2023
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the name of the department having the least number of employees working in it. We shall use the TOP, COUNT, ORDER BY, and GROUP BY clauses to achieve this. This is illustrated below. For this article, we will be using the Microsoft SQL Server as our database.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table COMPANY inside the database GeeksForGeeks. This table has 4 columns namely EMPLOYEE_ID, EMPLOYEE_NAME, DEPARTMENT_NAME, and SALARY containing the id, name, department, and the salary of various employees.
Query:
CREATE TABLE COMPANY(
EMPLOYEE_ID INT PRIMARY KEY,
EMPLOYEE_NAME VARCHAR(10),
DEPARTMENT_NAME VARCHAR(10),
SALARY INT);
Output:

Step 4: Describe the structure of the table COMPANY.
Query:
EXEC SP_COLUMNS COMPANY;
Output:

Step 5: Insert 5 rows into the COMPANY table.
Query:
INSERT INTO COMPANY VALUES(1,'RAM','HR',10000);
INSERT INTO COMPANY VALUES(2,'AMRIT','MRKT',20000);
INSERT INTO COMPANY VALUES(3,'RAVI','HR',30000);
INSERT INTO COMPANY VALUES(4,'NITIN','MRKT',40000);
INSERT INTO COMPANY VALUES(5,'VARUN','IT',50000);
Output:

Step 6: Display all the rows of the COMPANY table.
Query:
SELECT * FROM COMPANY;
Output:

Step 7: Display the name of the department having the least number the employees working in it. We will use the aggregate function COUNT here to find the number of employees working in each department. This is further achieved using the GROUP BY clause to count the number department-wise. Then all such obtained frequencies are sorted in increasing order using ORDER BY. Then the topmost row is selected using the TOP clause followed 1. 1 here indicates that the query returns only 1 row. We also name the new column of the least number of employees as MIN_NO_OF_EMPLOYEES using the AS clause which creates kind of an alias.
Syntax:
SELECT TOP 1 COLUMN1, COUNT(*) AS ALIAS
FROM TABLE_NAME
GROUP BY COLUMN1
ORDER BY COUNT(*);
Query:
SELECT TOP 1 DEPARTMENT_NAME, COUNT(*) AS MIN_NO_OF_EMPLOYEES
FROM COMPANY
GROUP BY DEPARTMENT_NAME
ORDER BY COUNT(*);
Note: This query returns only 1 row i.e. the topmost row among the returned sorted rows.
Output:

Method #2: Using Subquery
SELECT department_name
FROM employees
GROUP BY department_name
HAVING COUNT(*) = (SELECT MIN(emp_count) FROM (SELECT COUNT(*) AS emp_count FROM employees GROUP BY department_name) AS counts)
Method 3: Using a Common Table Expression (CTE)
A common table (CTE) is a temporary result set that can be referenced in SQL statements. This allows you to define named queries that can be used repeatedly within query blocks, making complex queries easier to read and maintain.
Syntax:
WITH CTE_Name (column1, column2, ..., columnN) AS (
-- CTE query definition here
SELECT ...
FROM ...
WHERE ...
)
SELECT ...
FROM ...
JOIN CTE_Name ON ...
WHERE ...
Query:
WITH EmployeeCounts AS (
SELECT DEPTNAME, COUNT(*) AS EmployeeCount
FROM DEPARTMENT
GROUP BY DEPTNAME
)
SELECT DEPTNAME, EmployeeCount
FROM EmployeeCounts
WHERE EmployeeCount = (SELECT MIN(EmployeeCount) FROM EmployeeCounts);
Output:

sql output
Similar Reads
Displaying Department Name Having Most Number of Employees in SQL Server
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the name of the department having the most number of employees working in it. We shall use the TOP, COUNT, ORDER BY, GROUP BY, and DESC clauses to achieve this. T
3 min read
Displaying Department Name Having Highest Average Salary in SQL Server
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the name of the department having the highest average salary of employees working in it. We shall use the TOP, AVG, ORDER BY, AS, GROUP BY, and DESC clauses to ac
3 min read
SQL Query to Find Names of the Employees Whose Department Have Number of Employees Less than 2
In SQL, we need to find out the department wise information from the given table containing information about employees. One such data is the name of employee who belong to a department having less than 2(i.e. only 1) employees. We shall use the GROUP BY, IN and COUNT clause to achieve this. This is
2 min read
Displaying the Employees in Decreasing Order of their Salaries in SQL Server
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the details of the employees sorted in decreasing order of their salaries. We shall use the ORDER BY clause to achieve this. This is illustrated below. For this a
2 min read
Displaying the Employees in Increasing Order of their Salaries in SQL Server
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the name of the department having the highest average salary of employees working in it. We shall use the ORDER BY clause to achieve this. This is illustrated bel
2 min read
Finding Average Salary of Each Department in SQL Server
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the average salary obtained by the employees in each department. We shall use the GROUP BY and AVG clause to achieve this. This is illustrated below. For this art
2 min read
SQL Query to Display Last 5 Records from Employee Table
In SQL, retrieving the last few records from a table can be crucial for various database management tasks such as auditing, pagination, and displaying recent entries. Whether you're dealing with user activity logs, recent transactions, or just viewing the latest additions to a database, SQL provides
5 min read
SQL Query to Print Name of Distinct Employee Whose DOB is Between a Given Range
Query in SQL is like a statement that performs a task. Here, we need to write a query that will print the name of the distinct employee whose DOB is in the given range. We will first create a database named âgeeksâ then we will create a table âdepartmentâ in that database. Creating a Database : Use
2 min read
SQL Query to Print the Name and Salary of the Person Having Least Salary in the Department
In SQL, we need to find out the department wise information from the given table containing information about employees. One such data is the minimum salary of the employees of each department. We shall use the GROUP BY clause to achieve this. This is illustrated below. For this article, we will be
2 min read
SQL Query to Display First 50% Records from Employee Table
Here, we are going to see how to display the first 50% of records from an Employee Table in MS SQL server's databases. For the purpose of the demonstration, we will be creating an Employee table in a database called "geeks". Creating a Database : Use the below SQL statement to create a database call
2 min read