Professional Documents
Culture Documents
lm 5
.rm 75
.mt 5
.mb 3
.pl 72
.he *************Developed by Anil Pal **************************
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 clerk whose salary is more than 800 & not working for deptno 10.
SQL> select * from emp where sal>800 and deptno not in(10);
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 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;
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;
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 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';
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;
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.
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
Trigger created.
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.
Table created.
Trigger created.
1 row created.
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.
Trigger created.