You are on page 1of 12

.

lm 5
.rm 75
.mt 5
.mb 3
.pl 72
.he *************Developed by Anil Pal **************************

SQL (DBMS) Practical

List the name & employee code of the employee whose salary is more than 1500.
SQL> select ename, empno from emp where sal>1500;

List the name & salary of the employee who is working in deptno 30.
SQL> select ename, sal from emp where deptno in(30);

List the name & salary of the employee who is working as an Analyst in deptno 10.
SQL> select ename, sal from emp where deptno=10 and job='ANALYST';

List the name & job of the employee whos salary more than 1000 but less than 2000.
SQL> select ename,job,sal from emp where sal>1000 and sal<2000;
.pa
Š
List all the deptno from EMP table.
SQL> select distinct(deptno) from emp;

List the name & salary of the employee who is working in deptno10, 20, and 30.
SQL> select ename, sal from emp where deptno in(10,20,30);

List the name & salary of the employee who is not working in deptno10, 20.
SQL> select ename,sal from emp where deptno not in(10,20);

List all the analyst who are working in deptno 20.


SQL> select * from emp where deptno=20 and job='ANALYST';
.pa
ŠDisplay the following out put.
SCOTT IS A MANAGER IN DEPARTMENT NUMBER 10
SQL> select ename||' IS A '||job||' IN DEPARTMENT NUMBER '||deptno from emp;

List all the clerk whose salary is more than 800 & not working for deptno 10.
SQL> select * from emp where sal>800 and deptno not in(10);

Give bonus of Rs 500 to all employees working for deptno 30.


SQL> update emp set sal=sal+500 where deptno=30;

Find the total salary of the each employee working for deptno 20.
SQL> select sum(sal) from emp where deptno=20;
SQL> select sum(sal) from emp group by deptno having deptno=20;
.pa
ŠFind the oldest employee.
SQL> SQL> select ename from emp where hiredate=(select min(hiredate) from emp);

List the name & salary of the employee whose salary is more than 1000 & working either in dept 10 or dept
20.
SQL> select ename, sal, from emp where sal>1000 and deptno in(10,20);
List the name & salary of the employee who are getting no commission for dept 10.
SQL> select ename, sal from emp where comm is null and deptno=10;

List the name & employee code of the employee whose salary is not in the range of 1000 & 1800.
SQL> select ename,empno, sal from emp where sal<1000 or sal>1800;
.pa
ŠFind all employee whose salary may be 1000, 800, 12000, 23000.
SQL> select * from emp where sal=1000 or sal=800 or sal=12000 or sal=23000;

List the Eid & job of the employee whose salary > 2000 & name starts with S.
SQL> select empno, job from emp where sal>2000 and ename like 'S%';

List all employees who joined in 1981.


SQL> select * from emp where hiredate like '%-81';

Display the salary of employee in decreasing order with their name.


SQL> select ename, sal from emp order by sal desc;
.pa
ŠList all the employee whose name contain one A & of dept 20.
SQL> select * from emp where deptno=20 and ename like '%A%';

List all the employee whose name contain OO & are manager.
SQL> select * from emp where job='MANAGER' and ename like '%oo%';

List all the employee whose name contain one A at third position in its name.
SQL> select * from emp where ename like '__A%';

List all the employee whose name start with S & end with T, of five characters.
SQL> select * from emp where ename like 'S___T';

List name, salary the employee whose name start with C and salary in 1000-2000 in range.
SQL> select ename, sal from emp where sal between 1000 and 2000 and ename like 'C%';
.pa
ŠFind highest salary.
SQL> select max(sal) from emp;

Find the employees who are working in CLARK’S department.


SQL> select ename from emp where deptno=(select deptno from emp where ename='CLARK');

Find the total salary of dept no 10.


SQL> select sum(sal) from emp group by deptno having deptno=10;

Find the minimum salary paid employee.


SQL> select min(sal) from emp;

Find top 3 salary paid employee.


SQL> select ename from (select * from emp order by sal desc) where rownum<4;
.pa
ŠSingle Row Function Queries

Find all salesmen earning more than 1000 or earning no commission.


SQL>select * from emp where job='SALESMAN' and ( sal>1000 or comm is null);

List all the employee, sort them job wise in ascending order & department wise in descending order.
SQL> select * from emp order by job,deptno desc;
List all employees whose name starts with A & end with A, sort them according to salary in descending
orders.
SQL> select ename from emp where ename like 'A%A' order by sal desc;

List first character of each employee name.


SQL> select substr(ename,1,1) from emp;

List employee name & salary having at least 5 character name.


SQL> select ename, sal from emp where ename like '%_____;
.pa
ŠList all palindrome name employee.
SQL> select ename from emp where ename in (select reverse(ename) from emp);

Find the current date.


SQL> select sysdate from dual;

Generate emailed of all the employee having ‘first 2 character of name’ then ’_’ then ‘last 2 character of
job’ & then ‘@oracle.com’.
SQL> select substr(ename,1,2) ||'_'|| substr(job,4,2) ||'@oracle.com' from emp;

Find Analyst & Clerk of department 10 earning more than 1000


SQL> select * from emp where (job='CLERK' or job='AYALYST') and deptno=10 and sal>1000;

Find the second highest salary paid employee.


SQL> select max(sal) from emp where sal!=(select max(sal) from emp);
.pa
ŠGroup Function Queries

List various jobs.


SQL> select job from emp group by job;

List total salary of each department.


SQL> select deptno,sum(sal+nvl(comm,0)) from emp group by deptno;

List average salary of department 10 & 20.


SQL> select deptno, avg(sal) from emp group by deptno having deptno=10 or deptno=20;

Find total salary of each job


SQL> select job, sum(sal+nvl(comm,0)) from emp group by job;
.pa
ŠFind minimum salary of each department
SQL> select min(sal) from emp group by deptno;

Find job & total salary of employee whose total salary more than 500.
SQL> select job,sum(sal+nvl(comm,0)) from emp group by job having sum(sal+nvl(comm,0))>500;

Find deptno & total salary of employee who are working for deptno 10.
SQL> select sum(sal+nvl(comm,0)), deptno from emp group by deptno having deptno=10;

Find deptno & total salary of employee who are not working for deptno 20
SQL> select sum(sal+nvl(comm,0)), deptno from emp group by deptno having deptno!=20;
.pa
ŠJoin Queries

Find ename, salary, deparment name of the employee who is working in New York
SQL> select e.ename,e.sal,d.dname from emp e inner join dept d on e.deptno=d.deptno and d.loc='NEW
YORK';

Display the department name of the deparment no 10 using emp table


SQL> select d.deptno, d.dname from emp e inner join dept d on e.deptno=d.deptno and e.deptno=10;

Find the grade of each employee.


SQL> select e.ename,g.grade from emp e inner join salgrade g on e.sal between g.losal and g.hisal;

Find the grade of each employee & are Clerk.


SQL> select e.ename, g.grade from emp e inner join salgrade g on e.sal between g.losal and g.hisal and
e.job='CLERK';

Find the grade of each employee. & in Sales department


SQL> select e.ename, g.grade from emp e inner join salgrade g on e.sal between g.losal and g.hisal and
deptno=(select deptno from dept where dname='SALES');

Find the employee along with their Managers.


SQL> select e1.ename, e2.ename as "MGR" from emp e1, emp e2 where e1.mgr=e2.empno;
.pa
ŠSub Queries

List the employee working in Smith’s dept.


SQL> select * from emp where deptno=(select deptno from emp where ename='SMITH');

List the employee earning more than all Clerks


SQL> select * from emp where sal>(select max(sal) from emp where job='CLERK');

List average salary of only those deptno whose average salary more than 1500
SQL> select deptno,avg(sal) from emp group by deptno having avg(sal)>1500;

List deptno whose salary is greater than 1500 & of deptno 30


SQL> select deptno from emp where sal>1500 and sal>(select max(sal) from emp where deptno=30);
.pa
ŠList employee with their salary & max salary of company
SQL> select ename, sal, (select max(sal) from emp) from emp;

Find the Allen’s department name


SQL> select dname from dept where deptno=(select deptno from emp where ename='ALLEN');

Find maximum salary from employee of each dept.


SQL> select ename, sal, deptno from emp where sal in (select max(sal) from emp group by deptno);
.pa
Š
PL SQL Programs

Write a PL/SQL program to perform arithmetic operation.


SQL>declare
2 a number(3);
3 b number(3);
4 c number(3);
5 begin
6 a:=7;
7 b:=4;
8 c:=a+b;
9 dbms_output.put_line('Sum= '||c);
10* end;
SQL> /
Sum= 11
PL/SQL procedure successfully completed.
.pa
Š
Write a PL/SQL program to find the given number is even or odd.
SQL>declare
2 num number(5);
3 begin
4 num:=&num;
5 if mod(num,2)=0
6 then
7 dbms_output.put_line('Given number is even');
8 else
9 dbms_output.put_line('Given number is odd');
10 endif;
11* end;
SQL> /
Enter value for num: 11
old 4: num:=&num;
new 4: num:=11;
Given number is odd

PL/SQL procedure successfully completed.


.pa
ŠWrite a PL/SQL program to find the area of circle.
SQL>declare
2 pi CONSTANT number(9,3):= 3.1415;
3 r integer(5);
4 area number(14,2);
5 begin
6 r:=&r;
7 area:=pi*r*r;
8 dbms_output.put_line('Area of circle='||area);
9* end;
SQL> /
Enter value for r: 11
old 6: r:=&r;
new 6: r:=11;
Area of circle=380.18

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to find the greater number among three numbers.
SQL>declare
a integer;
b integer;
c integer;
begin
a:=&a;
b:=&b;
c:=&c;
if a>b and a>c then
dbms_output.put_line('Greatest no. is A ');
else
if b>c then
dbms_output.put_line('Greatest no. is B');
else
dbms_output.put_line('Greatest no. is C');
end if;
end if;
end;
SQL> /
Enter value for a: 11
old 6: a:=&a;
new 6: a:=15;
Enter value for b: 1
old 7: b:=&b;
new 7: b:=1;
Enter value for c: 9
old 8: c:=&c;
new 8: c:=14;
Greatest no. is A

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to demonstrate the use of For Loop & Nested For Loop.
SQL>begin
for i in 1..3 loop
dbms_output.put_line('&&&&&');
for j in 1..5 loop
dbms_output.put_line('*****');
end loop;
end loop;
end;
SQL> /
&&&&&
*****
*****
*****
*****
*****
&&&&&
*****
*****
*****
*****
*****
&&&&&
*****
*****
*****
*****
*****

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to generate the table of given number.
SQL>declare
num integer;
begin
num:=&num;
for i in 1..10 loop
dbms_output.put_line(num*i);
end loop;
end;
SQL> /
Enter value for num: 13
old 4: num:=&num;
new 4: num:=13;
13
26
39
52
65
78
91
104
117
130

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to demonstrate the use of reverse For Loop.
SQL>begin
for i in reverse 1..5 loop
dbms_output.put_line('counter = '||i);
end loop;
end;
SQL> /
counter = 5
counter = 4
counter = 3
counter = 2
counter = 1

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to demonstrate the use of While Loop.
SQL>declare
a integer:=0;
begin
while a<=10 loop
dbms_output.put_line('The value of a is '||a);
a := a+1;
end loop;
end;
SQL> /
The value of a is 0
The value of a is 1
The value of a is 2
The value of a is 3
The value of a is 4
The value of a is 5
The value of a is 6
The value of a is 7
The value of a is 8
The value of a is 9
The value of a is 10

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to demonstrate the use of Select Case statement.
SQL>SELECT ename,job,
CASE deptno
WHEN 10
THEN 'ACCOUNTS'
WHEN 20
THEN 'SALES'
WHEN 30
THEN 'RESEARCH'
WHEN 40
THEN 'OPERATIONS
ELSE 'UNKNOWN'
END AS department
FROM emp
SQL> /

ENAME JOB DEPARTMENT


---------- --------- ----------
SMITH CLERK SALES
ALLEN SALESMAN RESEARCH
WARD SALESMAN RESEARCH
JONES MANAGER SALES
MARTIN SALESMAN RESEARCH
BLAKE MANAGER RESEARCH
CLARK MANAGER ACCOUNTS
SCOTT ANALYST SALES
KING PRESIDENT ACCOUNTS
TURNER SALESMAN RESEARCH
ADAMS CLERK SALES
JAMES CLERK RESEARCH
FORD ANALYST SALES
MILLER CLERK ACCOUNTS

14 rows selected.
.pa
ŠWrite a PL/SQL program to find sum of two number using stored procedure
SQL>create or replace procedure sum( a in number, b in
number, total out number) as
begin
total:=a+b;
dbms_output.put_line('Sum='||total);
end;
SQL> variable t number;
SQL> exec sum(4,7,:t);
Sum=11
PL/SQL procedure successfully completed.
.pa
Š
Write a PL/SQL program to find result of the student using stored procedure. Pass the following arguments
rno, name, phy, chm, mat as parameter calculate total and display result accordingly.
SQL>create or replace procedure info( rno in number, nm in
varchar2, phy in number, chm in number, mat in number,
total out number) as
begin
dbms_output.put_line('Student Roll Number='||rno);
dbms_output.put_line('Student Name='||nm);
dbms_output.put_line('Physics='||phy);
dbms_output.put_line('Chemistry='||chm);
dbms_output.put_line('Maths='||mat);
total:=phy+chm+mat;
if total<=60 and total>40 then
dbms_output.put_line('Pass in Division');
else
if total>=60 then
dbms_output.put_line('Pass in First Division');
else
dbms_output.put_line('Fail in exam');
end if;
end if;
end;
SQL> /

Procedure created.

SQL> variable t number;


SQL> exec info(101,'Nirmal', 35,35,45,:t);
Student Roll Number=101
Student Name=Nirmal
Physics=35
Chemistry=35
Maths=45
Pass in First Division

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to find the factorial of any given number using stroed procedure
SQL>create or replace procedure fact(n in number, facto out
number) as
begin
facto:=1;
if n<=0 then
facto:=1;
else
for i in reverse 1..n loop
facto:=facto*i;
end loop;
end if;
dbms_output.put_line('Factorial of Given number='||facto);
end;
SQL> /
Procedure created.

SQL> variable t number;


SQL> exec fact(5,:t);
Factorial of Given number=120

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program to find cube of a given number using function.
SQL>create or replace function cube1(a in integer) return
integer as
ans integer;
begin
ans:=a*a*a;
return ans;
end;
SQL> /

Function created.

SQL>declare
c integer;
begin
c:=cube1(&n);
dbms_output.put_line('Cube=='||c);
end;
/
Enter value for n: 4
old 4: c:=cube1(&n);
new 4: c:=cube1(4);
Cube==64

PL/SQL procedure successfully completed.


.pa
Š
Write a PL/SQL program for a trigger which fires only when authorized user accesses the table.
SQL> create or replace trigger trig
after insert on t2
for each row
begin
if uid=59 then
dbms_output.put_line('WELCOME');
else
dbms_output.put_line('YOU ARE UNAUTHORIZED USER');
end if;
end;
SQL> /

Trigger created.

SQL> insert into t2 values(101,'Priya', 450);


WELCOME

1 row created.
.pa
Š
Write a PL/SQL program for a trigger which fires only when user inserts the record into the table & store
userid,
date & operation in another table.
SQL> create table student (Rno number(3),Name varchar2(20),
Marks number(3));

Table created.

SQL> create table auditlog(id number(3),opdate date, opname


varchar2(10));

Table created.

SQL> create or replace trigger trg


after insert on student
for each row
begin
insert into auditlog values(uid, sysdate, 'insert');
end;
/

Trigger created.

SQL> insert into student values(101,'Priya',406);

1 row created.

SQL> select * from auditlog;

ID OPDATE OPNAME
---------- --------- ----------
59 07-APR-11 insert
.pa
Š
Write a PL/SQL program for a trigger which replicates the deleted record into another table only when user
deletes the records from the original table.

SQL>create or replace trigger del_std


after delete on student
for each row
begin
dbms_output.put_line('Rno='||:old.rno);
dbms_output.put_line('Name='||:old.name);
dbms_output.put_line('Marks='||:old.marks);
insert into del_student values(:old.rno,:old.name,
: old.marks);
end;
SQL> /

Trigger created.

SQL> set serveroutput on;


SQL> insert into student values(101,'Asmi',455);
1 row created.
SQL> insert into student values(102,'Priya',489);
1 row created.
SQL> insert into student values(103,'Kajal',499);
1 row created.
SQL> insert into student values(104,'Kadam Sir',201);
1 row created.
SQL> insert into student values(105,'Deepika',450);
1 row created.
SQL> insert into student values(106,'Shobna',502);
1 row created.
SQL> select * from student;

RNO NAME MARKS


---------- -------------------- ----------
101 Asmi 455
102 Priya 489
     103 Kajal 499
     104 Kadm Sir 201
105 Deepika 450
106 Shobna 502
.pa
Š

You might also like