SQL New
SQL New
1.List the project number, controlling department number and manager Name,
address.
SELECT P.NUM,P.CONTROLLING_DEPARTMENT,E.NAME,E.DOORNO
FROM PROJECT P,EMPLOYEE E
WHERE E.SSN=ANY(SELECT MANAGER_SSN FROM DEPARTMENT WHERE NUM=P.CONTROLLING_DEPAR
TMENT);
2.List the name and address of all employees who work for MCA Department.
SELECT FNAME,DOORNO
FROM EMPLOYEE
WHERE DEPARTMENT=(SELECT NUM FROM DEPARTMENT WHERE NAME='MCA');
3.List the project numbers that involve an employee whose last name is Smith
SELECT PROJECTNUM
FROM WORKS_ON W
WHERE W.SSN IN(SELECT SSN FROM EMPLOYEE WHERE LNAME='SMITH');
4.Retrieve the Names of the employees whose salary is greater than the salary
of all employees in department no 5.
SELECT FNAME
FROM EMPLOYEE E
WHERE E.SALARY>(SELECT MAX(SALARY) FROM EMPLOYEE WHERE DEPARTMENT=5);
5.Retrieve the employee name who has a dependent with same first Name and
same sex as the employee.
SELECT FNAME
FROM EMPLOYEE E
WHERE E.SSN IN (SELECT EMPLOYEE_SSN FROM DEPENDENT WHERE NAME=E.NAME AND SEX=E.S
EX);
6.Retrieve the name of each employee who works on all projects Controlled by
department number 5.
SELECT FNAME
FROM EMPLOYEE E
WHERE E.SSN IN (SELECT SSN FROM WORKS_ON W WHERE W.PROJECTNUM IN (SELECT NUM FRO
M PROJECT WHERE CONTROLLING_DEPARTMENT=5));
7.Retrieve the employee names who have no dependents.
SELECT FNAME
FROM EMPLOYEE E
WHERE E.SSN NOT IN(SELECT EMPLOYEE_SSN FROM DEPENDENT);
8.List the managers who have at least one dependent.
SELECT FNAME
FROM EMPLOYEE E
WHERE E.SSN IN (SELECT EMPLOYEE_SSN FROM DEPENDENT WHERE EMPLOYEE_SSN IN(SELECT
MANAGER_SSN FROM DEPARTMENT));
9.Retrieve the employee names who have at least two dependents.
SELECT FNAME
FROM EMPLOYEE E
WHERE (SELECT COUNT(*) FROM DEPENDENT WHERE S.SSN=EMPLOYEE_SSN)>=2);
10.Retrieve the project number, project name and the number of Employees
working on that project.
SELECT NUM,NAME,COUNT(*)
FROM PROJECT P,WORKS_ON W
WHERE P.NUM=W.PROJECTNUM
GROUP BY P.NUM,P.NAME;
11.Retrieve the project number, name and number of employees for which more
than two employees are working.
SELECT NUM,NAME,COUNT(*)
FROM PROJECT,WORKS_ON
WHERE NUM=PROJECTNUM
GROUP BY NUM,NAME
HAVING COUNT(*)>2;
12.Retrieve the department number and number of employees for which more
than five employees are working with salary > 40000.
SELECT NAME,COUNT(*)
FROM DEPARTMENT AND SALARY>40000
GROUP BY NAME
HAVING COUNT(*)>5;
13.Find the employee names who are working for the projects owned by the
department which is making the highest revenue from sponsored projects.
SELECT FNAME FROM EMPLOYEE
WHERE DEPARTMENT IN (SELECT CONTROLLING_DEPARTMENT FROM PROJECT WHERE BUDGET=(SE
LECT MAX(BUDGET) FROM PROJECT));
VI MISCELLANEOUS
1.Create an empty table empl with same structure as employee table.
CREATE TABLE EMPL(SSN,FNAME,MINIT,LNAME,ADDRESS,SALARY,DOB,DEPARTMENT,DESIGNATIO
N,SUPERVISOR_SSN)
AS (SELECT * FROM EMPLOYEE);
2.Create a table dp with same contents as dependent table.
CREATE TABLE DP(NAME,DOB,SEX,RELATIONSHIP,EMPLOYEE_SSN)
AS(SELECT * FROM DEPARTMENT);
3.Create a table with status department name, no of employees, total Salary.
Insert values into this table from existing tables.
CREATE TABLE STATUS
( DEPT_NAME VARCHAR2(30),
NO_EMP NUMBER(4),
TOTALSAL NUMBER(7)
);
INSERT INTO STATUS(DEPT_NAME,NO_EMP,TOTALSAL)
SELECT NAME,COUNT(*),SUM(SALARY)
FROM(DEPARTMENT JOIN EMPLOYEE ON NUM=DEPARTMENT)
GROUP BY NAME;
(OR)
CREATE TABLE STATUS AS SELECT NAME DNAME,COUNT(SSN) EMP_COUNT,SUM(SAL) TOT_SAL
FROM EMPLOYEE E LEFT OUTER JOIN DEPARTMENT ON E.DEPARTMENT=NUM
GROUP BY DEPARTMENT,NAME;
VII PL/SQL
1.Write a PL/SQL block to display the reverse of numbers between 1 and 100.
DECLARE
i number:=1;
n number:=1;
r number:=0;
d number:=0;
BEGIN
while i<=100 loop
r:=0;
d:=0;
n:=i;
while n>0 loop
d:=n510;
r:=(r*10)+d;
n:=n/10;
end loop;
dbms_output.put_line(r);
i:=i+1;
end loop;
END;
/
output
-------
1
2
3
4
5
6
7
8
9
01
11
21
31
.
.
.
79
89
99
001
PL/sql procedure successfully completed
2.Write a PL/SQL block to find the greatest of three numbers.
DECLARE
a number;
b number;
c number;
BEGIN
a:=&a;
b:=&b;
c:=&c;
if (a>b and a>c) then
dbms_output.put_line("the greatest number is"||a);
elsif (b>a and b>c) then
dbms_output.put_line("the greatest number is"||b);
else
dbms_output.put_line("the greatest number is"||c);
endif;
END;
/
output
-------
Enter the value of a:10
Enter the value of b:20
Enter the value of c:30
the greatest number is 30
PL/sql procedure successfully completed
output
-------
PL/sql procedure successfully completed
4. Write a block to raise an exception if the employee dob is not less than 14
years from today date.
declare
emp employee.ssn%type;
ename employee.name%type;
begin
emp:=&emp;
select fname into ename from employee
where (select floor(months_between(sysdate,dob)/12)
from employee where ssn=emp)>14 and ssn=emp;
dbms_output.put_line(fname);
exception
when no_data_found then
dbms_output.put_line("the dob is less than 14 years from today's date");
end;
output
-------
Enter the value of emp: 1222
the dob is less than 14 years from today's date
PL/sql procedure successfully completed
5. Write a cursor to give the details of the entire project.
declare
cursor proj is select * from project;
pr project%rowtype;
begin
if proj%isopen then
dbms_output.put_line("cursor opened");
else
dbms_output.put_line("cursor not opened");
endif;
open proj;
loop
fetch proj into pr;
exit when proj%notfound;
dbms_output.put_line(pr);
endloop;
close a;
end;
6. Write a cursor to give department details that range between 1234 and 1900.
declare
cursor dept is select * from department where num between 1234 and 1900;
dep department%rowtype;
begin
open dept;
loop
fetch dept into dep;
exit when dept%notfound;
dbms_output.put_line(dep);
endloop;
close dept;
end;
/
output
-------
PL/sql procedure successfully completed
7. Write a procedure to accept a employee name and display his Dependent
names, relationship and project names, controlling Department.
create or replace procedure emp_details(ename employee.fname%type) is
cursor a is select essn,name,relationship from dependent;
cursor b is select num,name,controlling_department from project;
dessn dependent.essn%type;
dname dependetnt.name%type;
relation dependent.relationship%type;
ppno works_on.projectnum%type;
pnum works_on.projectnum%type;
pname project.name%type;
cd project.controlling_department%type;
num employee.ssn%type;
begin
select ssn into num from employee where fname=ename;
open a;
loop
fetch a into dessn,dname,relation;
exit when a%notfound;
if(dessn=num) then
dbms_output.put_line("dependent name is"||dname);
dbms_output.put_line("relationship is "||relation);
endif;
endloop;
close a;
select project num into pnum from works_on where ssn=num;
open b;
loop
fetch b into ppno,pname,cd;
exit when b%notfound;
if(pnum=ppno) then
dbms_output.put_line("project name is"||pname);
dbms_output.put_line("controlling department is "||cd);
endif;
endloop;
close b;
end;
/
output
-------
procedure created
8. Write a function to give the number of projects for a given department name.
create or replace function no_project(dep department.name%type)
return number is
no number;
c number;
begin
select num into no from department where name=dep;
select count(num) int c from project where controlling_department=no;
return c;
end;
/
9. Write a function to return the department name which is Controlling highest
number of projects.
create or replace function highest()
return varchar2 is
n department.name%type;
no department.num%type;
c number;
begin
select max(count(num)) into c from project group by controlling_departme
nt;
select controlling_department into no from project group by controlling_
department having count(num)=c;
select name int n from department where num=no;
return n;
end;
/
output
-------
function created
10. Write a function to return the number of employees working in the department
which is making the highest revenue from the Sponsored projects.
create or replace function revenue()
return number is
c number;
r project.budget%type;
no department.num%type;
begin
select max(budget) into r from project;
select contolling_department into no from project where budget=r;
select count(*) into c from employee where department=no;
return no;
end;
/
output
-------
function created
11. Write a trigger to update the department number in employee table when
parent table is updated.
create or replace trigger dept
after update on department for each row
begin
update employee set :old.department=:new.department;
end;
/
output
-------
Trigger created
12. Write a trigger to delete all the foreign key references when the when the
parent primary key is deleted.
create or replace trigger del
before delete on department for each row
begin
delete from employee where department=:old department;
end;
/
output
-------
Trigger created
13. Write a trigger to update the status table when employee tuple is inserted.
create or replace trigger st
after insert on employee for each row
declare
c number;
tot employee.salary%type;
n varchar2;
begin
select sum(salary) into tot from employee where num=:new.department;
select name into n from department where num=:new.department;
select count(ssn) into c from employee where department=:new.department
update status set emp_count=c where dname=n;
update sataus set tot_sal=tot where dname=n;
end;
/
output
-------
Trigger created
14. Write a trigger to insert the current date as join date for a manager when
department record is inserted and display his age.
create or replace trigger emp
after insert on employee for each row
declare
c number;
d date;
begin
select sysdate into d from dual;
update employee set doj=d where :new.designation='manager';
select floor(months_between(sysdate,dob)/12) into c where :new.designation='mana
ger';
dbms_output.put_line("age is"||c);
end;
/
output
-------
Trigger created