SQL Assignment
You are provided with two tables:
1. Employees :
| EmployeeID | Name | Department | Salary | JoinDate |
2. Projects :
| ProjectID | ProjectName | Department | Budget |
DataSets:
https://drive.google.com/drive/folders/16fkU2DQumhYik0Ft6FG07QT32E3DpI3K?usp=sha
ring
Tasks
Task 1: Create the Tables and Insert Data
Write SQL scripts to create the Employees and Projects tables and insert the sample
data.
Task 2: Retrieve All Employees in the IT Department
Write a query to list all employees who work in the IT department.
Solution:
SELECT *
FROM Employees
WHERE Department = 'IT';
Task 3: Find Employees Who Joined After 2020
Retrieve the names of employees who joined after January 1, 2020.
Solution:
SELECT Name
FROM Employees
WHERE JoinDate > '2020-01-01';
Task 4: Calculate Total Budget by Department
Write a query to calculate the total project budget for each department.
Solution:
SELECT Department, SUM(Budget) AS TotalBudget
FROM Projects
GROUP BY Department;
Task 5: List Employees with a Salary Above the Average
Retrieve the details of employees whose salaries are above the average salary of all
employees.
Solution:
SELECT *
FROM Employees
WHERE Salary > (SELECT AVG(Salary) FROM Employees);
Task 6: Find Departments Without Projects
Write a query to find departments in the Employees table that have no associated projects in
the Projects table.
Solution:
SELECT DISTINCT Department
FROM Employees
WHERE Department NOT IN (SELECT DISTINCT Department FROM Projects);
Task 7: Add a Column for Performance Rating
Modify the Employees table to add a column PerformanceRating (default value as NULL),
and update it as follows:
HR employees: Rating = 4
IT employees: Rating = 5
Solution:
ALTER TABLE Employees ADD PerformanceRating INT;
UPDATE Employees
SET PerformanceRating = 4
WHERE Department = 'HR';
UPDATE Employees
SET PerformanceRating = 5
WHERE Department = 'IT';
Task 8: Display Employees and Their Project Details
Join the Employees and Projects tables to display each employee’s name, department, and
the project(s) associated with their department.
Solution:
SELECT e.Name, e.Department, p.ProjectName, p.Budget
FROM Employees e
LEFT JOIN Projects p ON e.Department = p.Department;
Additional Challenge
Task 9: Create a View for High Earners Create a view HighEarners that contains the
details of employees earning more than 60,000.
Solution:
CREATE VIEW HighEarners AS
SELECT *
FROM Employees
WHERE Salary > 60000;