Program 2
Program 2
USE CompanyDB;
SET FOREIGN_KEY_CHECKS = 0;
sex CHAR(1),
super_ssn CHAR(9),
address VARCHAR(100),
dno INT
);
mgr_ssn CHAR(9)
);
dnumber INT,
dloc VARCHAR(50),
);
-- Create the Project table
plocation VARCHAR(50),
dnum INT
);
essn CHAR(9),
pno INT,
);
essn CHAR(9),
depen_name VARCHAR(50),
address VARCHAR(100),
relationship VARCHAR(20),
sex CHAR(1),
);
SET FOREIGN_KEY_CHECKS = 1;
-- Insert sample data into Employee table (without supervisor and department initially)
INSERT INTO Employee (ssn, name, salary, sex, super_ssn, address, dno) VALUES
('111111111', 'John Doe', 75000.00, 'M', NULL, '123 Main St', NULL),
('222222222', 'Jane Smith', 80000.00, 'F', NULL, '456 Elm St', NULL),
('333333333', 'Alice Johnson', 90000.00, 'F', NULL, '789 Oak St', NULL),
('444444444', 'Bob Brown', 85000.00, 'M', NULL, '321 Pine St', NULL),
('555555555', 'Charlie Davis', 70000.00, 'M', NULL, '654 Cedar St', NULL);
UPDATE Employee
UPDATE Employee
('HR', 1, '111111111'),
('IT', 2, '222222222'),
('Finance', 3, '333333333');
UPDATE Employee
SET dno = 1
UPDATE Employee
SET dno = 2
WHERE ssn IN ('222222222', '555555555');
UPDATE Employee
SET dno = 3
(3, 'Chicago');
('111111111', 1, 20.0),
('222222222', 2, 30.0),
('333333333', 3, 25.0),
('444444444', 4, 15.0),
('555555555', 5, 10.0);
-- a. Retrieve the names of the Employees who work on all the projects controlled by dept no 3.
SELECT e.name
FROM Employee e
SELECT p.pnumber
FROM Project p
WHERE p.dnum = 3
SELECT w.essn
FROM Works_On w
);
-- b. Retrieve the names of the Employees who get the second highest salary.
SELECT name
FROM Employee
WHERE salary = (
FROM Employee
LIMIT 1 OFFSET 1
);
-- c. Retrieve the names of the Employees who have no dependents in alphabetical order.
SELECT e.name
FROM Employee e
SELECT d.essn
FROM Dependent d
SELECT e.name
FROM Employee e
WHERE (
SELECT COUNT(*)
FROM Dependent d
) >= 2;
-- e. Retrieve the number of Employees and their average salary working in each Department.
FROM Department d
LEFT JOIN Employee e ON d.dnumber = e.dno
GROUP BY d.dname;
FROM Department d
GROUP BY d.dname
-- g. Retrieve the SSN of all Employees who work on at least one of the project numbers 1, 2, 3.
FROM Works_On w
FROM Dependent d
-- i. Retrieve the names of the managers working in location named xyz who have no female
dependents.
SELECT e.name
FROM Employee e
SELECT dep.essn
);
-- j. Retrieve the names of the Employees who work in the same Department as that of RAM.
SELECT e.name
FROM Employee e
WHERE e.dno = (
SELECT dno
FROM Employee
);
-- k. Retrieve the names of the Employees whose salary is greater than the salary of all the
Employees working in Department no 3.
SELECT e.name
FROM Employee e
SELECT salary
FROM Employee
WHERE dno = 3
);
-- l. Retrieve the names of the Employees who work for dept no 3 and have a daughter as
dependent.
SELECT e.name
FROM Employee e
WHERE e.dno = 3
SELECT e.name
FROM Employee e
JOIN (
FROM Employee
GROUP BY dno
-- n. Retrieve the names of the Employees who are paid the same salary as that of Anil.
SELECT e.name
FROM Employee e
WHERE e.salary = (
SELECT salary
FROM Employee
);
FROM Employee e
-- p. For each project, retrieve the project number, the project name, and the number of Employees
who work on that project.
FROM Project p