SQL Practice 18-7-14 (Solved)
SQL Practice 18-7-14 (Solved)
Write and execute the necessary SQL statements to perform the following tasks.
1. Select the last name of all employees
SELECT LastName FROM Employees;
5. Select all the data of employees, including each employee's department's data.
SELECT SSN, E.Name AS Name_E, LastName, D.Name AS Name_D, Department, Code,
Budget
FROM Employees E INNER JOIN Departments D
ON E.Department = D.Code;
6. Select the name and last name of each employee, along with the name and budget of the
employee's department.
7. Select the name and last name of employees working for departments with a budget
greater than $60,000.
8. Select the departments with a budget larger than the average budget of all the
departments.
SELECT *
FROM Departments
WHERE Budget >
(
SELECT AVG(Budget)
FROM Departments
);
9. Select all the data of employees that work in department 37 or department 77.
SELECT * FROM Employees WHERE Department = 37 OR Department = 77;
10. Select all the data of employees whose last name begins with an "S".
12. Select the number of employees in each department (you only need to show the
department code and the number of employees).
13. Select the names of departments with more than two employees.
14. Add a new department called "Quality Assurance", with a budget of $40,000 and
departmental code 11.
15. Add an employee called "Mary Moore" in that department, with SSN 847-2198-110.
19. Find all employees who work in departments with a budget greater than or equal to
$60,000.
SELECT Name, LastName FROM Employees
WHERE Department IN
(SELECT Code FROM Departments WHERE Budget >= 60000)
21. Select the departments with a budget larger than the average budget of all the departments.
SELECT *
FROM Departments
WHERE Budget >
(
SELECT AVG(Budget)
FROM Departments
);