Extra Credit Assignment - SQL Implementation
Table Creation and Data Insertion
-- Create Employee Table
CREATE TABLE Employee (
Empid INT PRIMARY KEY,
EmpName VARCHAR(50),
Department VARCHAR(10),
ContactNo VARCHAR(15),
EmailId VARCHAR(50),
EmpHeadId INT
);
INSERT INTO Employee VALUES
(101, 'Isha', 'E-101', '1234567890', '
[email protected]', 105),
(102, 'Priya', 'E-104', '1234567890', '
[email protected]', 103),
(103, 'Neha', 'E-101', '1234567890', '
[email protected]', 101),
(104, 'Rahul', 'E-102', '1234567890', '
[email protected]', 105),
(105, 'Abhishek', 'E-101', '1234567890', '
[email protected]', 102);
CREATE TABLE EmpDept (
DeptId VARCHAR(10) PRIMARY KEY,
DeptName VARCHAR(50),
Dept_off VARCHAR(15),
DeptHead INT
);
INSERT INTO EmpDept VALUES
('E-101', 'HR', 'Monday', 105),
('E-102', 'Development', 'Tuesday', 101),
('E-103', 'House Keeping', 'Saturday', 103),
('E-104', 'Sales', 'Sunday', 104),
('E-105', 'Purchase', 'Tuesday', 104);
CREATE TABLE EmpSalary (
EmpId INT PRIMARY KEY,
Salary INT,
IsPermanent VARCHAR(5)
);
-
INSERT INTO EmpSalary VALUES
(101, 2000, 'Yes'),
(102, 10000, 'Yes'),
(103, 5000, 'No'),
(104, 1900, 'Yes'),
(105, 2300, 'Yes');
CREATE TABLE Project (
ProjectId VARCHAR(10) PRIMARY KEY,
Duration INT
);
INSERT INTO Project VALUES
('p-1', 23),
('p-2', 15),
('p-3', 45),
('p-4', 2),
('p-5', 30);
CREATE TABLE EmpProject (
EmpId INT,
ProjectId VARCHAR(10),
ClientID VARCHAR(10),
StartYear INT,
EndYear INT
);
INSERT INTO EmpProject VALUES
(101, 'p-1', 'Cl-1', 2010, 2010),
(102, 'p-2', 'Cl-2', 2010, 2012),
(103, 'p-1', 'Cl-3', 2013, NULL),
(104, 'p-4', 'Cl-1', 2014, 2015),
(105, 'p-4', 'Cl-5', 2015, NULL);
Queries
-- 1. Select the detail of the employee whose name starts with 'P'.
SELECT * FROM Employee WHERE EmpName LIKE 'P%';
-- 2. Select the detail of employees whose EmailId is in Gmail.
SELECT * FROM Employee WHERE EmailId LIKE '%@gmail.com%';
-- 3. Select the details of employees who work either for department E-104 or E-102.
SELECT * FROM Employee WHERE Department IN ('E-104', 'E-102');
-- 4. What is the department name for DeptID E-102?
SELECT DeptName FROM EmpDept WHERE DeptId = 'E-102';
-- 5. List the names of all employees whose names end with 'a'.
SELECT EmpName FROM Employee WHERE EmpName LIKE '%a';
-- 6. Select the name of employees whose name's 3rd character is 'h'.
SELECT EmpName FROM Employee WHERE SUBSTRING(EmpName, 3, 1) = 'h';
-- 7. How many permanent candidates take salaries more than 5000?
SELECT COUNT(*) FROM EmpSalary WHERE IsPermanent = 'Yes' AND Salary > 5000;
-- 8. What is the total salary paid to permanent employees?
SELECT SUM(Salary) FROM EmpSalary WHERE IsPermanent = 'Yes';
-- 9. List the number of departments of employees in each project.
SELECT EmpProject.ProjectId, COUNT(DISTINCT Employee.Department) AS DeptCount
FROM EmpProject
JOIN Employee ON EmpProject.EmpId = Employee.Empid
GROUP BY EmpProject.ProjectId;
-- 10. How many projects started and finished in the same year?
SELECT COUNT(*) FROM EmpProject WHERE StartYear = EndYear;