Intern — Middle
SQL query to count the number of employees in each department
livecode
Task condition
There are two tables: divisions (list of divisions) and employees (list of employees). It is required to write an SQL query that will display the name of each division and the number of employees belonging to it. The expected result: | division_name | employees_count | | IT Department | 28 |
CREATE TABLE divisions (id PRIMARY KEY, name);
INSERT INTO divisions (id, name)
VALUES
(1, 'IT Department'),
(2, 'Information Security Department'),
(3, 'HR Department');
CREATE TABLE employees (
id PRIMARY KEY,
division_id,
name,
surname,
mail,
CONSTRAINT division_fk
FOREIGN KEY (division_id)
REFERENCES divisions(id)
);
INSERT INTO employees (id, division_id, name, surname, mail)
VALUES
(1, 1, 'Name', 'Surname', 'e-mail@yandex.ru'),
(2, 1, 'Name1', 'Surname1', 'e-mail1@yandex.ru'),
(3, 1, 'Name2', 'Surname2', 'e-mail2@yandex.ru'),
(4, 1, 'Name3', 'Surname3', 'e-mail3@yandex.ru'),
(5, 1, 'Name4', 'Surname4', 'e-mail4@yandex.ru'),
(6, 1, 'Name5', 'Surname5', 'e-mail5@yandex.ru'),
(7, 1, 'Name6', 'Surname6', 'e-mail6@yandex.ru'),
(8, 1, 'Name7', 'Surname7', 'e-mail7@yandex.ru'),
(9, 3, 'Name8', 'Surname8', 'e-mail8@yandex.ru');