Middle+
Task 3 Given the following SQL database structure --Table Departments create table D ( Id int primary key, Name nvarchar(100), ) --Table Employees create table E ( Id int primary key, DepartmentId int foreign key references Departments(Id), Name nvarchar(100), Salary float, ) Required: a. Write a query to get the name of one employee with the highest salary in the company 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 name of the employee with the highest salary and the name of their department, you can use the following SQL query:
SELECT E.Name AS EmployeeName, D.Name AS DepartmentName
FROM Employees E
JOIN Departments D ON E.DepartmentId = 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, the query would be:
SELECT D.Name AS DepartmentName
FROM Departments D
JOIN Employees E ON E.DepartmentId = D.Id
GROUP BY D.Id, D.Name
HAVING AVG(E.Salary) > 1000
These queries use aggregate functions and joins to retrieve the necessary data.