Last active
October 1, 2024 14:55
-
-
Save OlegPetrenkoGit/0adb08c24b4f0a03fba9 to your computer and use it in GitHub Desktop.
Sql interview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 1. Вывести список сотрудников, получающих заработную плату большую чем у непосредственного руководителя | |
SELECT * | |
FROM Employee AS employees, Employee AS chieves | |
WHERE chieves.id = employees.chief_id AND employees.salary > chieves.salary; | |
-- 2. Вывести список сотрудников, получающих максимальную заработную плату в своем отделе | |
SELECT * | |
FROM Employee AS employees | |
WHERE employees.salary = (SELECT MAX(salary) FROM Employee AS max WHERE max.department_id = employees.department_id); | |
-- 3. Вывести список ID отделов, количество сотрудников в которых не превышает 3 человек | |
SELECT department_id | |
FROM Employee | |
GROUP BY department_id | |
HAVING COUNT(*) <= 3; | |
--4. Вывести список сотрудников, не имеющих назначенного руководителя, работающего в том-же отделе | |
SELECT * | |
FROM Employee AS employees | |
LEFT JOIN Employee AS chieves ON (employees.chief_id = chieves.Id AND employees.department_id = chieves.department_id) | |
WHERE chieves.id IS NULL; | |
--5. Найти список ID отделов с максимальной суммарной зарплатой сотрудников | |
WITH dep_salary AS | |
(SELECT department_id, sum(salary) AS salary | |
FROM employee | |
GROUP BY department_id) | |
SELECT department_id | |
FROM dep_salary | |
WHERE dep_salary.salary = (SELECT max(salary) FROM dep_salary); |
-- Задание 5 Найти список ID отделов с максимальной суммарной зарплатой сотрудников --
select department_id, sum(salary) as salary from employee
group by department_id having sum(salary) >= all(select sum(salary) as salary from employee group by department_id);
Это задание можно сделать следующим образом без использования WITH AS
в 1 запросе можно объединить таблицу саму на себя через JOIN
и улыбнуло "chieves" прям как "thieves" . Верно chiefs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
А структура бд?