-- SQL Solution for Employee and Department Database Operations
-- ================================================
-- Step 1: Create Employees and Departments Tables with specified columns and
constraints
CREATE TABLE Departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL
);
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
salary DECIMAL(10, 2),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);
-- Step 2: Insert Data into Departments and Employees Tables
INSERT INTO Departments (dept_id, dept_name) VALUES
(1, 'Administration'),
(2, 'Customer Service'),
(3, 'Finance'),
(4, 'Human Resources'),
(5, 'Sales');
INSERT INTO Employees (emp_id, emp_name, salary, dept_id) VALUES
(1, 'Ethan Hunt', 5000, 4),
(2, 'Tony Montana', 6500, 1),
(3, 'Sarah Connor', 8000, 5),
(4, 'Rick Deckard', 7200, 3),
(5, 'Martin Blank', 5600, NULL);
-- Step 3: Left Join Query to retrieve employee id, name along with department name
SELECT e.emp_id, e.emp_name, d.dept_name
FROM Employees AS e
LEFT JOIN Departments AS d ON e.dept_id = d.dept_id;
-- Step 4: Create a View for the query above
CREATE VIEW EmployeeDepartmentView AS
SELECT e.emp_id, e.emp_name, d.dept_name
FROM Employees AS e
LEFT JOIN Departments AS d ON e.dept_id = d.dept_id;
-- Step 5: Retrieve records from the view
SELECT * FROM EmployeeDepartmentView;
-- Step 6: Alter the view to add salary in the view
CREATE OR REPLACE VIEW EmployeeDepartmentView AS
SELECT e.emp_id, e.emp_name, d.dept_name, e.salary
FROM Employees AS e
LEFT JOIN Departments AS d ON e.dept_id = d.dept_id;
-- Step 7: Insert 3 more records into Employees table and view the updated data
INSERT INTO Employees (emp_id, emp_name, salary, dept_id) VALUES
(6, 'Neo', 6500, 2),
(7, 'Trinity', 7000, 2),
(8, 'Morpheus', 5800, 3);
SELECT * FROM EmployeeDepartmentView;
-- Step 8: Update salary to 6000 for employee with emp_id = 5
UPDATE Employees SET salary = 6000 WHERE emp_id = 5;
SELECT * FROM EmployeeDepartmentView;
-- Step 9: Retrieve records where department id of employees is NULL
SELECT * FROM EmployeeDepartmentView WHERE dept_name IS NULL;
-- Step 10: Delete records from view where employee salary is 8000
DELETE FROM Employees WHERE salary = 8000;
SELECT * FROM EmployeeDepartmentView;
-- Step 11: Drop the view
DROP VIEW EmployeeDepartmentView;
-- ================================================
-- Additional Queries with Employee and Project Tables
-- ================================================
-- Step 12: Create Employee and Project Tables with specified columns
CREATE TABLE ProjectLocations (
proj_id INT PRIMARY KEY,
addr VARCHAR(50) NOT NULL
);
CREATE TABLE Staff (
eid INT PRIMARY KEY,
ename VARCHAR(50) NOT NULL,
addr VARCHAR(50),
salary DECIMAL(10, 2),
commission DECIMAL(10, 2)
);
-- Step 13: Insert Data into Staff and ProjectLocations Tables
INSERT INTO Staff (eid, ename, addr, salary, commission) VALUES
(1, 'Amit', 'Pune', 35000, 5000),
(2, 'Sneha', 'Pune', 25000, NULL),
(3, 'Savita', 'Nasik', 28000, 2000),
(4, 'Pooja', 'Mumbai', 19000, NULL),
(5, 'Sagar', 'Mumbai', 25000, 3000),
(6, 'Rohit', 'Jaipur', 40000, NULL),
(7, 'Poonam', 'Patna', 45000, 2000),
(8, 'Arjun', 'Delhi', 20000, 900),
(9, 'Rahul', 'Nagpur', 60000, 5000),
(10, 'Dulquer', 'Kochi', 30000, 1000);
INSERT INTO ProjectLocations (proj_id, addr) VALUES
(10, 'Mumbai'),
(20, 'Pune'),
(30, 'Jalgaon'),
(40, 'Nagpur'),
(50, 'Delhi'),
(60, 'Kochi'),
(70, 'Pune'),
(80, 'Nasik');
-- Step 14: Additional Queries
-- 1. Find different locations from where employees belong to
SELECT DISTINCT addr FROM Staff;
-- 2. Maximum, minimum, average salary, and sum of all salaries
SELECT MAX(salary) AS MaxSalary, MIN(salary) AS MinSalary, AVG(salary) AS
AvgSalary, SUM(salary) AS TotalSalary FROM Staff;
-- 3. Content of employee table in ascending order of salary
SELECT * FROM Staff ORDER BY salary ASC;
-- 4. Employee names who live in Nasik or Pune
SELECT ename FROM Staff WHERE addr IN ('Nasik', 'Pune');
-- 5. Employees who do not get commission
SELECT ename FROM Staff WHERE commission IS NULL;
-- 6. Update the city of Amit to Nashik
UPDATE Staff SET addr = 'Nashik' WHERE ename = 'Amit';
SELECT * FROM Staff WHERE ename = 'Amit';
-- 7. Employees whose name starts with 'A'
SELECT * FROM Staff WHERE ename LIKE 'A%';
-- 8. Count of staff from Mumbai
SELECT COUNT(*) AS StaffFromMumbai FROM Staff WHERE addr = 'Mumbai';
-- 9. Count of staff from each city
SELECT addr, COUNT(*) AS StaffCount FROM Staff GROUP BY addr;
-- 10. Addresses where employees belong and projects are located (Union operation)
SELECT addr FROM Staff
UNION
SELECT addr FROM ProjectLocations;