assignment-sql
assignment-sql
3. Count the number of employees in each department, but only show departments with
more than 3 employees
SELECT department, COUNT(*) AS employee_count
FROM employee
GROUP BY department
HAVING COUNT(*) > 3;
4. Find employees earning more than the average salary of all employees
SELECT first_name, last_name, salary
FROM employee
WHERE salary > (SELECT AVG(salary) FROM employee);
9. Find the employee who has been working for the longest time
SELECT first_name, last_name, hire_date
FROM employee
ORDER BY hire_date ASC
LIMIT 1;
11. Calculate the cumulative salary per department using a window function
SELECT department, first_name, last_name, salary,
SUM(salary) OVER (PARTITION BY department ORDER BY salary DESC) AS
cumulative_salary
FROM employee;
12. Find employees who earn more than the average salary of their own department
SELECT first_name, last_name, department, salary
FROM employee e1
WHERE salary > (SELECT AVG(salary) FROM employee e2 WHERE e1.department =
e2.department);
13. Find the difference between the highest and lowest salaries in each department
SELECT department,
MAX(salary) - MIN(salary) AS salary_difference
FROM employee
GROUP BY department;
14. Find the department where the total salary is more than 200,000
SELECT department, SUM(salary) AS total_salary
FROM employee
GROUP BY department
HAVING SUM(salary) > 200000;
16. Find employees who have the same salary as another employee
SELECT e1.first_name, e1.last_name, e1.salary
FROM employee e1
JOIN employee e2 ON e1.salary = e2.salary AND e1.employee_id <> e2.employee_id;
20. Find employees hired before the average hire date of all employees
SELECT first_name, last_name, hire_date
FROM employee
WHERE hire_date < (SELECT AVG(hire_date) FROM employee);