Count Employees Per Department

Easy

Description

Write a SQL query to count the number of employees in each department. Return department and count, ordered by count descending. **Output columns (in order):** department, count

Table:employees
ColumnType
idINT
nameTEXT
departmentTEXT

Examples

Input:CREATE TABLE employees (id INT, name TEXT, department TEXT); INSERT INTO employees VALUES (1, 'Alice', 'Engineering'), (2, 'Bob', 'Marketing'), (3, 'Charlie', 'Engineering');
Output:Engineering|2 Marketing|1
Explanation:

Alice and Charlie are in Engineering (2 employees), Bob is in Marketing (1 employee). Results ordered by count descending.

Input:CREATE TABLE employees (id INT, name TEXT, department TEXT); INSERT INTO employees VALUES (1, 'David', 'Sales'), (2, 'Emma', 'HR'), (3, 'Frank', 'Sales'), (4, 'Grace', 'Finance'), (5, 'Henry', 'Sales'), (6, 'Ivy', 'HR');
Output:Sales|3 HR|2 Finance|1
Explanation:

Sales department has the most employees (3), followed by HR with 2 employees, and Finance with 1 employee. Results are ordered by count in descending order.

Input:CREATE TABLE employees (id INT, name TEXT, department TEXT); INSERT INTO employees VALUES (1, 'John', 'Operations'), (2, 'Kate', 'Operations'), (3, 'Liam', 'Operations'), (4, 'Maya', 'Operations');
Output:Operations|4
Explanation:

All employees belong to the same department (Operations), so there's only one row in the result showing Operations with 4 employees total.

Constraints

  • Use GROUP BY

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!