0% found this document useful (1 vote)
223 views

SQL Practice 18-7-14 (Solved)

The document contains the SQL code to create tables for employees and departments along with sample data. It then lists 21 SQL queries to retrieve, update, insert and delete data from the tables including selecting employee and department data based on various criteria, joining the tables, aggregating data, updating budgets and reassigning employees between departments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
223 views

SQL Practice 18-7-14 (Solved)

The document contains the SQL code to create tables for employees and departments along with sample data. It then lists 21 SQL queries to retrieve, update, insert and delete data from the tables including selecting employee and department data based on various criteria, joining the tables, aggregating data, updating budgets and reassigning employees between departments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Employees Table:

CREATE TABLE Employees (


SSN INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL ,
LastName TEXT NOT NULL ,
Department INTEGER NOT NULL ,
CONSTRAINT fk_Departments_Code FOREIGN KEY(Department)
REFERENCES Departments(Code)
);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('123234877','Michael','Rogers',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('152934485','Anand','Manikutty',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('222364883','Carol','Smith',37);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('326587417','Joe','Stevens',37);
INSERT INTO Employees(SSN,Name,LastName,Department) VALUES('332154719','Mary-
Anne','Foster',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('332569843','George','O''Donnell',77);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('546523478','John','Doe',59);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('631231482','David','Smith',77);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('654873219','Zacary','Efron',59);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('745685214','Eric','Goldsmith',59);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('845657245','Elizabeth','Doe',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('845657246','Kumar','Swamy',14);

SSN Name LastName Department


123234877 Michael Rogers 14
152934485 Anand Manikutty 14
222364883 Carol Smith 37
326587417 Joe Stevens 37
332154719 Mary -Anne Foster 14
332569843 George ODonnell 77
546523478 John Doe 59
631231482 David Smith 77
654873219 Zacary Efron 59
745685214 Eric Goldsmith 59
845657245 Elizabeth Doe 14
845657246 Kumar Swamy 14
Departments Table:

CREATE TABLE Departments (


Code INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL ,
Budget REAL NOT NULL
);

INSERT INTO Departments(Code,Name,Budget) VALUES(14,'IT',65000);


INSERT INTO Departments(Code,Name,Budget) VALUES(37,'Accounting',15000);
INSERT INTO Departments(Code,Name,Budget) VALUES(59,'Human
Resources',240000);
INSERT INTO Departments(Code,Name,Budget) VALUES(77,'Research',55000);

Code Name Budget


14 IT 65000
37 Accounting 15000
59 Human Resources 240000
77 Research 55000

Write and execute the necessary SQL statements to perform the following tasks.
1. Select the last name of all employees
SELECT LastName FROM Employees;

2. Select the last name of all employees, without duplicates


SELECT DISTINCT LastName FROM Employees;

3. Select all the data of employees whose last name is "Smith"


SELECT * FROM Employees WHERE LastName = 'Smith';

4. Select all the data of employees that work in department 14.


SELECT * FROM Employees WHERE Department = 14;

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.

SELECT Employees.Name, LastName, Departments.Name AS DepartmentsName, Budget


FROM Employees INNER JOIN Departments
ON Employees.Department = Departments.Code;

7. Select the name and last name of employees working for departments with a budget
greater than $60,000.

SELECT Name, LastName FROM Employees


WHERE Department IN
(SELECT Code FROM Departments WHERE Budget > 60000)

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".

SELECT * FROM Employees


WHERE LastName LIKE 'S%';

11. Select the sum of all the departments' budgets.

SELECT SUM(Budget) FROM Departments;

12. Select the number of employees in each department (you only need to show the
department code and the number of employees).

SELECT Department, COUNT(*)


FROM Employees
GROUP BY Department;

13. Select the names of departments with more than two employees.

SELECT Name FROM Departments


WHERE Code IN
(
SELECT Department
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 2
);

14. Add a new department called "Quality Assurance", with a budget of $40,000 and
departmental code 11.

INSERT INTO Departments


VALUES ( 11 , 'Quality Assurance' , 40000);

15. Add an employee called "Mary Moore" in that department, with SSN 847-2198-110.

INSERT INTO Employees


VALUES ( '847219811' , 'Mary' , 'Moore' , 11);

16. Reduce the budget of all departments by 10%.


UPDATE Departments SET Budget = Budget * 0.9;
17. Reassign all employees from the Research department (code 77) to the IT department
(code 14).

UPDATE Employees SET Department = 14 WHERE Department = 77;

18. Find all employees in the IT department (code 14).


SELECT * FROM Employees WHERE Department = 14;

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)

20. Select the sum of all the departments' budgets.

SELECT SUM(Budget) FROM Departments;

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
);

You might also like