Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 36

PART I SQL

Table rakjay
Eno 7369 7499 7521 7566 7654 7698 7782 7782 7902 Ename Sunita Ashok Rohit Jyoti Martin Binod Chetan Sudhir Sumit vats Job Clerk Salesman Salesman Manager salesman Manager Manager Analyst Analyst Mgr 7902 7698 7698 7839 7698 7839 7839 7566 7566 Hierdate 17-dec-80 20-feb-81 22-feb-81 02-apr-81 28-sep-81 01-may-81 09-jun-81 19-apr-87 03-dec-81 Sal 2800 3600 5250 4975 6250 5850 2450 5000 3500 Comm. 300 500 1400 Dept 20 30 30 20 30 30 10 20 20

3600

1. Create the above table and insert the same values.


SQL> create table rakjay(ename varchar(10), job varchar(10), eno number(5), mgr number(5), hierdate date, sal number(8), comm number(5), dept number(5)); Table created. SQL> desc rakjay; Name Null? Type ----------------------------------------- -------- ---------------------------ENAME VARCHAR2(10) JOB VARCHAR2(10) ENO NUMBER(5) MGR NUMBER(5) HIERDATE DATE SAL NUMBER(8) COMM NUMBER(5) DEPT NUMBER(5) SQL>insert into rakjay values ('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept'); Enter value for ename: Sunita Enter value for job: clerk Enter value for eno: 7369 Enter value for mgr: 7902 Enter value for hierdate: 17-dec-80 Enter value for sal: 2800 Enter value for comm: Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('Sunita','clerk','7369',' 7902',' 17-dec-80','2800','','20') 1 row created. SQL> / Enter value for ename: ashok Enter value for job: salesman Enter value for eno: 7499
2

Enter value for mgr: 7698 Enter value for hierdate: 20-feb-81 Enter value for sal: 3600 Enter value for comm: 300 Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('ashok','salesman','7499','7698','20-feb-81','3600','300','30') 1 row created. SQL> / Enter value for ename: rohit Enter value for job: salesman Enter value for eno: 7521 Enter value for mgr: 7698 Enter value for hierdate: 22-feb-81 Enter value for sal: 5250 Enter value for comm: 500 Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('rohit','salesman','7521','7698','22-feb-81','5250','500','30') 1 row created. SQL> / Enter value for ename: jyoti Enter value for job: manager Enter value for eno: 7566 Enter value for mgr: 7839 Enter value for hierdate: 02-apr-81 Enter value for sal: 4975 Enter value for comm: Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('jyoti','manager','7566','7839','02-apr-81','4975','','20') 1 row created. SQL> / Enter value for ename: martin Enter value for job: salesman Enter value for eno: 7654 Enter value for mgr: 7698 Enter value for hierdate: 28-sep-81 Enter value for sal: 6250 Enter value for comm: 1400 Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('martin','salesman','7654','7698','28-sep-81','6250','1400','30') 1 row created. SQL> / Enter value for ename: binod
3

Enter value for job: manager Enter value for eno: 7698 Enter value for mgr: 7839 Enter value for hierdate: 01-may-81 Enter value for sal: 5850 Enter value for comm: Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('binod','manager','7698','7839','01-may-81','5850','','30') 1 row created. SQL> / Enter value for ename: chetan Enter value for job: manager Enter value for eno: 7782 Enter value for mgr: 7839 Enter value for hierdate: 09-jun-81 Enter value for sal: 2450 Enter value for comm: Enter value for dept: 10 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('chetan','manager','7782','7839','09-jun-81','2450','','10') 1 row created. SQL> / Enter value for ename: sudhir Enter value for job: analyst Enter value for eno: 7781 Enter value for mgr: 7566 Enter value for hierdate: 19-apr-87 Enter value for sal: 5000 Enter value for comm: Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('sudhir','analyst','7782','7566','19-apr-87','5000','','20') 1 row created. SQL> / Enter value for ename: sumit vats Enter value for job: analyst Enter value for eno: 7902 Enter value for mgr: 7566 Enter value for hierdate: 03-dec-81 Enter value for sal: 3500 Enter value for comm: 3600 Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('sumit vats','analyst','7902','7566','03-dec-81','3500','3600','2 1 row created.
4

2. Select all the columns of the above table.


SQL> select * from rakjay; ENAME JOB ENO MGR HIERDATE SAL COMM ---------- ---------- ---------- ---------- --------- ---------- ---------DEPT ---------Sunita clerk 7369 7902 17-DEC-80 2800 20 ashok salesman 7499 7698 20-FEB-81 3600 300 30 rohit salesman 7521 7698 22-FEB-81 5250 500 30 ENAME JOB ENO MGR HIERDATE SAL COMM ---------- ---------- ---------- ---------- --------- ---------- ---------DEPT ---------jyoti manager 7566 7839 02-APR-81 4975 20 martin salesman 7654 7698 28-SEP-81 6250 1400 30 binod manager 7698 7839 01-MAY-81 5850 30 ENAME JOB ENO MGR HIERDATE SAL COMM ---------- ---------- ---------- ---------- --------- ---------- ---------DEPT ---------chetan manager 7782 7839 09-JUN-81 2450 10 sudhir analyst 7781 7566 19-APR-87 5000 20 sumit vats analyst 7902 7566 03-DEC-81 3500 3600 20 9 rows selected.

3. List the name & eno from the above table.


SQL> select ename, eno from rakjay; ENAME ENO ---------- ---------Sunita 7369 ashok 7499 rohit 7521 jyoti 7566 martin 7654 binod 7698 chetan 7782
5

sudhir sumit vats

7781 7902

4. Display the ename and the incremented value of sal as sal+300.


SQL> Select ename, sal+300 from rakjay; E_NAME increment

--------------- ---------sunita ashok rohit jyoti martin binod chetan sudhir sumit vats 3100 3900 5550 5275 6550 6150 2750 5300 3800

9 rows selected

5. Display the ename & sal where comm. Is null.


SQL> select e_name, sal from rakjay where comm is NULL; E_NAME SAL

--------------- ---------sunita jyoti binod chetan sudhir 2800 4975 5850 2450 5000

6. List the distinct deptno from the table.


SQL> select distinct (deptno) from rakjay; DEPTNO 6

---------10 20 30

7. List the unique jobs from the table.


SQL> select distinct (job) from rakjay; JOB -------------------analyst clerk manager salesman

8. List the sal where sal is less than the commission.


SQL> select sal from rakjay where sal<comm; SAL ---------3500

9. List the mgr which are in 7902,7566,7788


SQL> select e_name, mgr from rakjay where mgr in (7902,7566,7788); E_NAME MGR

--------------- ---------sunita sudhir sumit vats 7902 7566 7566

10.
E_NAME --------------sunita sudhir sumit vats

List the ename starting with s

SQL> select e_name from rakjay where e_name like 's%';

11.List all the columns where salary is greater than 4100.


SQL> select * from rakjay where sal>4100; E_NO E_NAME JOB MGR HIREDATE SAL

---------- --------------- -------------------- ---------- --------- ---------COMM DEPTNO

---------- ---------7521 rohit 500 30 manager 7839 02-APR-81 4975 salesman 7698 22-FEB-81 5250

7566 jyoti 20 7654 martin 1400 30

salesman

7698 28-SEP-81

6250

E_NO E_NAME

JOB

MGR HIREDATE

SAL

---------- --------------- -------------------- ---------- --------- ---------COMM DEPTNO

---------- ---------7698 binod 30 7781 hir analyst 7566 19-APR-87 5000 manager 7839 01-MAY-81 5850

12. List all the columns in the ascending order of hiredate.


SQL> select * from rakjay order by hiredate; E_NO E_NAME JOB MGR HIREDATE SAL

---------- --------------- -------------------- ---------- --------- ---------COMM DEPTNO

---------- ---------7369 sunita 20 7499 ashok 300 30 salesman 7698 22-FEB-81 5250 salesman 7698 20-FEB-81 3600 clerk 7902 17-DEC-80 2800

7521 rohit 500 30

E_NO E_NAME

JOB

MGR HIREDATE

SAL

---------- --------------- -------------------- ---------- --------- ---------COMM DEPTNO

---------- ---------7566 jyoti 20 7698 binod 30 7782 chetan 10 E_NO E_NAME JOB MGR HIREDATE SAL manager 7839 09-JUN-81 2450 manager 7839 01-MAY-81 5850 manager 7839 02-APR-81 4975

---------- --------------- -------------------- ---------- --------- ---------COMM DEPTNO

---------- ---------7654 martin 1400 30 analyst 7566 03-DEC-81 3500 9 salesman 7698 28-SEP-81 6250

7902 sumit vats

3600

20

7781 sudhir 20 9

analyst

7566 19-APR-87

5000

rows selected.

13. List all the details in the ascending order of deptno & descending order of salary.
SQL> select * from rakjay order by deptno,sal desc; E_NO E_NAME JOB MGR HIREDATE

---------- -------------------- -------------------- ---------- --------SAL COMM DEPTNO GRAD

---------- ---------- ---------- ---7782 chetan 2450 7781 sudhir 5000 7566 jyoti 4975 E_NO E_NAME 20 JOB MGR HIREDATE 20 manager 7839 02-APR-81 10 analyst 7566 19-APR-87 manager 7839 09-JUN-81

---------- -------------------- -------------------- ---------- --------SAL COMM DEPTNO GRAD

---------- ---------- ---------- ---7902 sumit vats 3500 3600 20 clerk 20 salesman 30 JOB MGR HIREDATE 7698 28-SEP-81 7902 17-DEC-80 analyst 7566 03-DEC-81

7369 sunita 2800 7654 martin 6250 1400

E_NO E_NAME

---------- -------------------- -------------------- ---------- --------10

SAL

COMM

DEPTNO GRAD

---------- ---------- ---------- ---7698 binod 5850 7521 rohit 5250 500 30 salesman 30 salesman 30 7698 20-FEB-81 7698 22-FEB-81 manager 7839 01-MAY-81

7499 ashok 3600 9 300

rows selected.

14. Display the ename & deptno of all employees in dept 20 &30 in alphabetical order by name.
SQL> select e_name, deptno from rakjay where deptno=20 or deptno=30 order by e_name; E_NAME DEPTNO

--------------- ---------ashok binod jyoti martin rohit sudhir sumit vats sunita 30 30 20 30 30 20 20 20

15. List the name & salary of all the employees who earn more than 1200 and are in dept 10 or 40.
SQL> select e_name, sal from rakjay where sal>1200 and (deptno=10 or deptno=40); E_NAME SAL

--------------- ---------chetan 2450

11

16. List the name of all employees where the second letter of their name is an a.
SQL> select e_name from rakjay where e_name like '_a%'; E_NAME --------------Martin

17. Display the difference of highest department having maximum salary >6000
difference ---------2650

&

lowest

salary

of

each

SQL> select (max(sal)-min(sal))"difference" from rakjay group by deptno having max(sal)>6000;

18. Display the name of employees whose name contains Mas first alphabet ,L as third alphabet.
SQL> select e_name from rakjay where e_name like 'm_l%'; no rows selected

19. Display ename, sal of those employees who do not have their salary in the range of 3000 to 6000.
SQL> select e_name, sal from rakjay where sal not between 3000 and 6000; E_NAME SAL

--------------- ---------sunita martin chetan 2800 6250 2450

20. Display the name of employee whose name contain M as first


SQL> select e_name from rakjay where e_name like 'm%'; E_NAME --------------Martin

12

21. Display total salary of all employees listed in table emp.


SQL> select sum(sal)"total salary" from rakjay; total salary -----------39675

22. List the maximum salary of employee SQL> select max(sal) from rakjay group by deptno; MAX(SAL) ---------2450 5000 6250

grouped by their department number.

23. Display the sum, average, highest and lowest salary of the employees grouped by department number.
SQL> select deptno, sum(sal), max(sal), min(sal), avg(sal) from rakjay group by deptno; DEPTNO SUM(SAL) MAX(SAL) MIN(SAL) AVG(SAL) ---------- ---------- ---------- ---------- ---------10 20 30 2450 16275 20950 2450 5000 6250 2450 2800 3600 2450 4068.75 5237.5

24. Add a field Grade in above table


SQL> alter table rakjay add(grade char(4)); Table altered. SQL> desc rakjay; Name Null? Type

----------------------------------------- -------- ---------------------ENAME JOB ENO VARCHAR2(10) VARCHAR2(10) NUMBER(5) 13

MGR HIREDATE SAL COMM DEPT GRADE

NUMBER(5) DATE NUMBER(8) NUMBER(5) NUMBER(5) CHAR(4)

25. Calcualte the average gross & total gross for employees belonging to E4 grade.
SQL> Select avg(sal), sum(sal) from rakjay where grade= E4;

26. To display the jobs where the number of employees is less than 3.
SQL> select job from rakjay group by job having count(job)<3 ; JOB -------------------analyst clerk

27. Create table rakjay with the following structure Name of column Type ID First_Name Last_name User_ID Salary NUMBER(4) Varchar2(30) Varchar2(30) Varchar2(10) Number(9,2)

Ensure the following specification in created table. ID should be declared as primary Key User_Id should be unique Salary must be greater than 5000 First_Name & last_Name must not remain blank.
SQL> create table rakjay(ID number(4) primary key, First_name varchar2(30) not null, last_name varchar2(30) not null, user_id varchar2(10) unique, salary number(9,2), check (salary>5000)); Table created. 14

SQL> desc rakjay; Name Null? Type

----------------------------------------- -------- ---------------------------ID FIRST_NAME LAST_NAME USER_ID SALARY NUMBER(4) NOT NULL VARCHAR2(30) NOT NULL VARCHAR2(30) VARCHAR2(10) NUMBER(9,2)

28. Delete the employee Record having ename as Jyoti.


SQL> delete from rakjay where e_name='jyoti'; 1 row deleted.

29. Rename rakjay to vr.


SQL> alter table rakjay rename to vr; Table altered. SQL> desc rakjay; ERROR: ORA-04043: object employee does not exist SQL> desc vr; Name Null? Type

----------------------------------------- -------- ---------------------ENAME JOB ENO MGR HIREDATE SAL COMM DEPT VARCHAR2(10) VARCHAR2(10) NUMBER(5) NUMBER(5) DATE NUMBER(8) NUMBER(5) NUMBER(5) 15

GRADE

CHAR(4)

30. Drop table vr.


SQL> drop table vr; Table dropped.

31. Change the data type of column ename to char(20) in the Table employee.
SQL> alter table rakjay modify(ename varchar(20)); Table altered. SQL> desc rakjay; Name Null? Type

----------------------------------------- -------- ---------------------ENAME JOB ENO MGR HIREDATE SAL COMM DEPT GRADE VARCHAR2(20) VARCHAR2(10) NUMBER(5) NUMBER(5) DATE NUMBER(8) NUMBER(5) NUMBER(5) CHAR(4)

32. Make the changes permanent.


SQL> Commit;

16

Q1. List the name and salary of the employees whose salary is more than 1000.
SQL> select ename, sal from emp where sal>1000; ENAME SAL

---------- ---------ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS FORD ENAME 1600 1250 2975 1250 2850 2450 5500 5000 1500 1100 5500 SAL

---------- ---------MILLER 1300

12 Rows selected.

Q2. List the employee number and name of manager.


SQL> select empno, ename from emp where job='MANAGER'; EMPNO ENAME ---------- ---------7566 JONES 7698 BLAKE 17

7782 RK

Q3. List the name of Clerks working in Department 20.


SQL> select ename from emp where job='CLERK' and deptno='20'; ENAME ---------SMITH ADAMS

Q4. List the employee name and salary, whose salary is between 1000 and 2000.
SQL> select ename, sal from emp where sal between 1000 and 2000; ENAME SAL

---------- ---------ALLEN WARD MARTIN TURNER ADAMS MILLER 6 1600 1250 1250 1500 1100 1300

rows selected.

Q5. List the names of analysts and salesman.


SQL> select ename, job from emp where job='ANALYST' or job='SALESMAN'; ENAME JOB

---------- --------ALLEN WARD MARTIN SCOTT TURNER FORD 6 SALESMAN SALESMAN SALESMAN ANALYST SALESMAN ANALYST rows selected.

18

Q6. List the details of Employees who have joined before end of September 81.
SQL> select ename from emp where hiredate<'30-sep-81' ENAME ---------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK TURNER

rows selected.

Q7. List the name of employees who are not managers.


SQL> select ename, job from emp where job !='MANAGER'; ENAME JOB

---------- --------SMITH ALLEN WARD MARTIN SCOTT KING TURNER ADAMS JAMES FORD MILLER CLERK SALESMAN SALESMAN SALESMAN ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK

11 rows selected

Q8. List the employees who are not eligible for commission.
SQL> select ename from emp where comm is NULL; ENAME ---------19

SMITH JONES BLAKE CLARK SCOTT KING ADAMS JAMES FORD MILLER

Q9. List the employees not assigned to any department.


SQL> select ename from emp where deptno is NULL; no rows selected

Q10. List the employees who are eligible for commission.


SQL> select ename from emp where comm is NOT NULL; ENAME ---------ALLEN WARD MARTIN TURNER

Q11. List the details of employees, whose salary is more than 2,500/- and commission is null.
SQL> select * from emp where sal>2500 and comm is NULL; EMPNO ENAME JOB MGR HIREDATE SAL ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO ---------7566 JONES 20 7698 BLAKE 30 20 MANAGER 7839 01-MAY-81 2850 MANAGER 7839 02-APR-81 2975 COMM

7788 SCOTT 20

ANALYST

7566 19-APR-87

5500

EMPNO ENAME JOB MGR HIREDATE SAL ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO ---------7839 KING 10 7902 FORD 20 ANALYST 7566 03-DEC-81 5500 PRESIDENT 17-NOV-81 5000

COMM

Q12. List the number of employees working with the company.


SQL> select count(ename) from emp; COUNT(ENAME) -----------14

Q13. List the number of jobs available in the emp table.


SQL> select count(job) from emp; COUNT(JOB) ---------14

Q14. List the maximum, minimum, average and sum of salary.


SQL> select max(sal), min(sal), avg(sal), sum(sal) from emp; MAX(SAL) MIN(SAL) AVG(SAL) SUM(SAL) ---------- ---------- ---------- ----------

5500

800 2430.35714

34025

Q15. List the maximum salary of the employee working as a Salesman.


SQL> select max(sal) from emp where job='SALESMAN'; MAX(SAL) ---------1600

21

Q16. List the average salary of number of employees working in department 20.
SQL> select avg(sal), count(ename) from emp where deptno=20; AVG(SAL) COUNT(ENAME) ---------- ------------

3175

Q17. List the department number and number of employees in each department.
SQL> select deptno, count(ename) from emp group by deptno; DEPTNO COUNT(ENAME) ---------- -----------10 20 3 5

30

Q18. List the department number and the total salary payable in each department.
SQL> select deptno, sum(sal) from emp group by deptno; DEPTNO SUM(SAL) ---------- ---------10 20 8750 15875

30

9400

Q19. Select the jobs that are unique to department 20.


SQL> select unique(job) from emp where deptno=20; JOB --------ANALYST CLERK MANAGER

Q20. List the employee names whose salary is greater than the lowest salary of an employee belonging to department number 20.
SQL> select ename from emp where sal>(select min(sal) from emp where deptno=20); ENAME ---------ALLEN WARD JONES MARTIN BLAKE 22

CLARK SCOTT KING TURNER ADAMS JAMES ENAME ---------FORD MILLER

13 rows selected.

Q21. List the employee names whose salary is greater than the highest salary of an employee belonging to department number 20.
SQL> select ename from emp where sal>(select max(sal) from emp where deptno=20); ENAME ---------KING

Q22. List the employees who joined in 1981.


SQL> select ename from emp where hiredate between '31-dec-80' and '01-jan-82'; ENAME ---------ALLEN WARD JONES MARTIN BLAKE CLARK KING TURNER JAMES FORD 10 rows selected.

Q23. Add number of month to a given date and returns a date after those many months.
SQL> select hiredate, ADD_months(hiredate, 6)"probation" from emp where ename='SMITH'; HIREDATE probation --------- --------23

17-DEC-80 17-JUN-81

Q24. Convert a date into equivalent character string.


SQL> select TO_char(hiredate, 'month,dd,yyyy') from emp where ename='SCOTT'; TO_CHAR(HIREDATE, ----------------april ,19,1987

Q25. Query to make the first letter capital.


SQL> select INITCAP('the soap') "capitals" from dual;

OUTPUT: capitals -------The Soap

Q26. Query to change the statement from uppercase to lower case.


SQL> select LOWER ('MR.SCOTT MCMILLAN') "LOWER" from dual;

OUTPUT: LOWER ----------------mr.scott mcmillan

Q27. Query to pad the left side of the string with the given set of characters.
SQL> select LPAD('AIMT',15,'*') "LPAD EXAMPLE" from DUAL;

OUTPUT:
24

LPAD EXAMPLE --------------***********AIMT

Q28. Query to remove all specified characters from the left side of the string.
SQL> SELECT LTRIM('ARMYINSTITUTE','ARMY') "LTRIM EXAMPLE" FROM DUAL;

OUTPUT: LTRIM EXA --------INSTITUTE

Q29. Query to remove all specified characters from the right side of the string.
SQL> SELECT RTRIM('ARMYINSTITUTE', 'INSTITUTE') "RTRIM EXAMPLE" FROM DUAL;

OUTPUT: RTRI ---ARMY

Q30. Query to write a string and grab a specified string from the given set of characters.
SQL> SELECT SUBSTR('ARMYINSTITUTE',6,4)"SUBSTR" FROM DUAL; OUTPUT:
25

SUBS ---NSTI

Q31. Query to replace characters.


SQL> SELECT REPLACE('JACK AND JUE','J','BL') "CHANGES" FROM DUAL;

OUTPUT: CHANGES -------------BLACK AND BLUE

Q32. Query to find out the starting location of the pattern of the string.
SQL> SELECT JOB,INSTR(JOB,'LE')FROM EMP;

OUTPUT: JOB INSTR(JOB,'LE')

--------- --------------CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK 2
26

2 3 3 0 3 0 0 0 0 3

JOB

INSTR(JOB,'LE')

--------- --------------CLERK ANALYST CLERK 2 2 0

14 rows selected.

PART II PL SQL
27

Q1. Write a PL/SQL program to read two numbers and display their sum.
Ans. declare num1 number(2); num2 number(2); num3 number(4); begin num1:=&number1; num2:=&number2; num3:=num1+num2; dbms_output.put_line('the sum is:'||num3); end; / Enter value for number1: 3 old 6: num1:=&number1; new 6: num1:=3; Enter value for number2: 4 old 7: num2:=&number2; new 7: num2:=4; the sum is:7 PL/SQL procedure successfully completed.
28

Q2. Write a program to read two numbers and display their square.
Ans. declare num1 number(2); sq number(6); begin num1:=&number1; sq:=num1*num1; dbms_output.put_line('the square is:'||sq); end; / Enter value for number1: 3 old 5: num1:=&number1; new 5: num1:=3; the square is:9 PL/SQL procedure successfully completed.

Q3. Program to display area of triangle.


Ans. declare num1 number(2); num2 number(2); num3 number(4); begin num1:=&number1; num2:=&number2; num3:=1/2*(num1*num2); dbms_output.put_line('the area of triangle is:'||num3); end; / Enter value for number1: 4 old 6: num1:=&number1;
29

new 6: num1:=4; Enter value for number2: 6 old 7: num2:=&number2; new 7: num2:=6; the area of triangle is:12 PL/SQL procedure successfully completed.

Q4. Program to display area of rectangle.


Ans. declare num1 number(2); num2 number(2); num3 number(4); begin num1:=&number1; num2:=&number2; num3:=num1*num2; dbms_output.put_line('the area of rectangle is:'||num3); end; / Enter value for number1: 3 old 6: num1:=&number1; new 6: num1:=3; Enter value for number2: 8 old 7: num2:=&number2; new 7: num2:=8; the area of rectangle is:24 PL/SQL procedure successfully completed.

Q5. Program to display sum, difference, multiplication and division.


Ans. declare A1 number(4);
30

B1 number(4); C1 number(4); D1 varchar(4); begin A1:=&number1; B1:=&number2; D1:=&varchar1; if D1:='+' then C1:=A1+B1; dbms_output.put_line('The sum is:'||C1); else if D1:='-' then C1:=A1-B1; dbms_output.put_line('The subtraction is:'||C1); else if D1:='*' then C1:=A1*B1; dbms_output.put_line('The multiplication is:'||C1); else D1:='/' C1:=A1/B1; dbms_output.put_line('The division is:'||C1); end if; end;

Q6. Program to display that if the number is greater than the other it swaps the two number otherwise it doubles them.
Ans. Declare X number(2); Y number(2); Z number(2); S number(2); D number(2); D1 number(4); Begin
31

X:=& number1; Y:=& number2; If X>Y Then Z:=X; X:=Y; Y:=Z; Dbms_output.put_line(X||Y); Else D=2XY; D1=2XY; Dbms_output.put_line(D||D1); End if; End;

Q7. Program to print first N natural number & their sum.


Ans. Declare N number; Sum number:=0; Begin N:=&number; For I IN 1.....N loop SUM=SUM+I; Dbms_output.put_line(som of first n natural number||SUM); End;

Q8. Program to print table


Ans. Declare N number(2); T number(2);
32

Begin N:=&num1; For I IN 1.....10 Loop T=N*I; Dbms_output.put_line(the table is:||T); End;

Q9. Program to print table through while loop.


Ans. Declare I number:=0; Begin While I<10 Loop I:=0+2; Dbms_output.put_line(I); End loop; End;

Q10. Write a PL/SQL code to increase the salary of employee whose salary is 3000.Get the employee number from the user.
Ans. Declare Empno number(4); Salary number(7,2); Begin Empno:=&empno; Select sal into salary from emp Where
33

Empno=eno; If salary>3000 Update emp set sal=sal+.05*sal; Where empno=eno; End if; End;

Q.11. Write a PL/SQL code that traps zero divide exceptions while calculating price to earnings ratio with a company symbol xyz.
Ans. Declare Pre ratio number(3,1); Begin Select price/earnings into price ratio from stock Where symbol=xyz; Exceptions When zero divides then Insert into stocks (symbol, ratio) values(xyz,NULL); Commit; When other Then rollback; End;

Q.12. Write a PL/SQL code to obtain details of employee drawing maximum salary. Add his/her details into a table new sal (empno, ename, salary) after incrementing salary by Rs.700.
Ans. Declare Eno emp.empno%type; Name emp.ename%type;
34

Salary emp.sal%type; Begin Select empno,ename,sal into eno,ename,salary from emp where sal=(select max(sal) from emp); Salary=salary+700; Insert into new sal values (Eno,ename,salary); End;

Q13. Write a PL/SQL code in reference with Explicit cursor.


Ans. Declare
Cursor emp_cur IS Select * from emp; Emprec emp% rowtype; Begin If not emp_cur IS open Then open emp_cur End if Fetch emp_cur into emprec While emp_cur % found Loop Dbms_output.put_line(emp_cur % row count); End loop; Close emp_cur; End;

Q14. Write a PL/SQL code in reference with implicit cursor.


Ans. Declare
My sql numer(7,2); Rec number; 35

Begin Select sal into my sql from emp where empno=7788; When sal>mysql IF sql % found Then Dbms_output.put_line(mysql) If sql % row count>0 Then Rec:=sql % row count; Dbms_output.put_line(rec); End if;

End;

36

You might also like