CREATE TABLE EMPLOYEE6
( Fname Char(15),
Minit Char(15),
Lname Char(15),
Ssn Number(5),
Bdate date,
Address char(15),
Gender char(2),
Salary number(5),
Super_ssn number(5),
Dno number(3),
PRIMARY KEY (Ssn));
insert into employee6 values('John','L',Smith',
1001,'10:01:1995','Blore','M',80,000,,,);
CREATE TABLE DEPARTMENT
( Dname Char(10),
Dnumber number(3),
Mgr_ssn number(5),
Mgr_start_date date,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE6(Ssn));
CREATE TABLE DEPT_LOCATIONS
( Dnumber number(3),
Dlocation Char(10),
PRIMARY KEY (Dnumber, Dlocation),
FOREIGN KEY (Dnumber) REFERENCES DEPARTMENT(Dnumber));
CREATE TABLE PROJECT
( Pname char(5),
Pnumber number(3),
Plocation Char(10),
Dnum number(3),
PRIMARY KEY (Pnumber),
UNIQUE (Pname),
FOREIGN KEY (Dnum) REFERENCES DEPARTMENT(Dnumber) );
CREATE TABLE WORKS_ON
( Essn number(10),
Pno number(3),
Hours number(3),
PRIMARY KEY (Essn, Pno),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE6(Ssn),
FOREIGN KEY (Pno) REFERENCES PROJECT(Pnumber) );
CREATE TABLE DEPENDENT
( Essn number(10),
Dependent_name char(10),
gender char(3),
Bdate date,
Relationship char(10),
PRIMARY KEY (Essn, Dependent_name),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE6(Ssn) );
q1: select Fname, Lname, Address from EMPLOYEE6 E, DEPARTMENT D where
Dname='Research' and D.Dnumber=E.Dno;
q2:SELECT Pnumber, Dnum, Lname, Address, Bdate from PROJECT, DEPARTMENT, EMPLOYEE6
where Dnum = Dnumber AND Mgr_ssn = Ssn AND
Plocation = 'Mumbai';
Q3:SELECT Fname as EmployeeFristName, Address from EMPLOYEE6, DEPARTMENT where
DEPARTMENT.DName = 'Research' AND
DEPARTMENT.Dnumber = EMPLOYEE6.Dno;
Q4:select Fname, Lname from employee6 where Super_ssn is null;
Q5:select distinct pnumber from
PROJECT, DEPARTMENT, EMPLOYEE6 WHERE Dno=Dnumber and mgr_ssn=ssn and
Lname='Smith')
or
(select Pnumber from PROJECT, WORKS_ON, EMPLOYEE6 WHERE Pnumber = Pno AND Essn =
ssn and Lname = 'Smith');
6:select distinct pnumber from
PROJECT where pnumber in (select pnumber from
PROJECT, DEPARTMENT, EMPLOYEE6 where Dno=Dnumber and mgr_ssn=ssn and fname='John')
or pnumber in
(select Pnumber from PROJECT, WORKS_ON, EMPLOYEE6 WHERE Essn = ssn and fname =
'John');
7:SELECT Lname, Fname
FROM EMPLOYEE6
WHERE Salary > ALL ( SELECT Salary
FROM EMPLOYEE6
WHERE Dno = 2 );
8: Retrieve the names of employees who have no dependents.
SELECT fname, lname from employee6 where NOT EXISTS
( SELECT * FROM DEPENDENT where Ssn = Essn )
9: List the names of managers who have at least one dependent
SELECT Fname, Lname FROM EMPLOYEE6
WHERE EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn = Essn )
AND
EXISTS ( SELECT * FROM DEPARTMENT WHERE Ssn = Mgr_ssn )
10:Retrieve the name of each employee who works on all the projects controlled by
department number 2
SELECT Fname, Lname FROM EMPLOYEE6 WHERE NOT EXISTS
((SELECT Pnumber FROM PROJECT WHERE Dnum = 2)
minus
(SELECT Pno FROM WORKS_ON WHERE Ssn = Essn));
11: Retrieve the Social Security numbers of all employees who work on project
numbers 1, 2, or 3
SELECT DISTINCT Essn FROM WORKS_ON WHERE Pno IN (1, 2, 3)
12:retrieve the last name of each employee and his or her supervisor while renaming
the resulting attribute names as Employee_name and Supervisor_name.
SELECT e1.Lname AS Supervisor_name , e2.Lname AS Employee_name FROM
EMPLOYEE6 e1, EMPLOYEE6 e2 where e2.super_ssn=e1.ssn;
13:Write the sql query to retrieves the name and address of every employee who
works for the ‘Research’ department using JOIN .
SELECT Fname, Lname, Address
FROM (EMPLOYEE6 JOIN DEPARTMENT ON Dno = Dnumber)
WHERE Dname = 'Research';
14:Join Qieries
select * from employee6 right join department on ssn=mgr_ssn;
select * from employee6 right outer join department on ssn=mgr_ssn;
select * from employee6 right inner join department on ssn=mgr_ssn;
select * from employee6 left join department on ssn=mgr_ssn;
select * from employee6 left inner join department on ssn=mgr_ssn;
select * from employee6 left outer join department on ssn=mgr_ssn;
15: SELECT FROM SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary) EMPLOYEE6;
16:SELECT SUM (Salary) AS Total_Sal, MAX (Salary) AS Highest_Sal,MIN (Salary) AS
Lowest_Sal, AVG (Salary) AS Average_Sal from EMPLOYEE6;
17:SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
from (EMPLOYEE6 JOIN DEPARTMENT ON Dno = Dnumber) where
Dname = 'Research';
18: select count(*) from employee6;
19: select count(*) from employee6, department where DNO = DNUMBER AND DNAME =
'Research';
20:SELECT Lname, Fname FROM EMPLOYEE6 where (SELECT COUNT (*) FROM DEPENDENT WHERE
Ssn=Essn )>=1;
21:
For each department, retrieve the department number, the number of employees in the
department, and their average salary.
SELECT Dno, COUNT (*), AVG (Salary)
FROM EMPLOYEE6 GROUP BY Dno;
22:For each project, retrieve the project number, the project name, and the number
of employees who work on that project
SELECT pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON where Pnumber = Pno GROUP
BY Pnumber, Pname;
23: For each project on which more than two employees work, retrieve the project
number, the project name, and the number of employees who work on
the project.
SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber = Pno GROUP
BY Pnumber, Pname
HAVING COUNT (*) > 2;
24: For each project, retrieve the project number, the project name, and
the number of employees from department 5 who work on the project
SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT, WORKS_ON, EMPLOYEE6
WHERE Pnumber = Pno AND Ssn = Essn AND Dno =2
GROUP BY Pnumber, Pname