Sobes.tech
Middle+

SQL task: a) Get the name of the employee with the highest salary and the name of their department; b) Get a list of departments where the average salary is more than $1000.

sobes.tech AI

Answer from AI

a) To get the employee's name with the highest salary and the name of their department, you can use a subquery to find the maximum salary and join with the departments table:

SELECT e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE e.salary = (SELECT MAX(salary) FROM employees);

b) To get a list of departments where the average salary is greater than $1000, use grouping and filtering by an aggregate function:

SELECT d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.id
GROUP BY d.department_name
HAVING AVG(e.salary) > 1000;

It is assumed here that there are tables employees with fields name, salary, department_id and departments with fields id, department_name.