Beruflich Dokumente
Kultur Dokumente
1 2 3 4 5 6 7 8 9
EN AME
aman simran karampreet manjot gurpreet gurnoor hardeep manjeet abhinav
S AL AR Y
15000 25000 12000 10000 17000 8000 11000 27000 15000
DE PT
10 20 30 30 40 10 30 50 60
DE SG
clerk manager salesman salesman clerk clerk salesman admin manager
CO MM
500 1000 200 200 500 200 200 -
BAS IC_P AY
1000 1500 500 500 1000 1000 500 2000 2000
2)Write a query to view selected columns and all rows Select rno, name from student; Output:R NO
1 2 3 4 5
N AM E
meenakshi sandeep sarbhjeet ekjot geetik
3)Write a query to view selected columns and selected rows Select name from stu30 where rollno=1; Output:N AME
meenaksh i
4)Create table student with columns rollno(primary key),name(not null),address,fathername,marks, phone no. and perform following queries on it :1.Insert 5 records in the table
1
2.Update rollno 2s name to Sandeep 3.Update the ph_no of any student. 4.Rollno and names of student whose marks >70 5.Show details of all students 6.Show the details of rollno =5 7.Delete any record create table stu30(rollno number(3) primary key,name varchar(10) NOT NULL,address varchar(10),fathername varchar(10),marks number(8),ph_no number(10)); desc stu30; Output:Tab le
STU 30
Co lu mn Da ta T yp e L en gt h P reci si on S cale
ROLLNO NAME ADDRESS Number Varchar2 Varchar2 10 10 10 3 8 10 0 0 0
P ri mary Key
1 -
Nu ll ab le Defaul t Comment
-
1.Insert 5 records in the table insert into stu30 values(1,'meenakshi','ldh','manish',68,84736); insert into stu30 values(2,'meenakshi','jldhr','manish',67,87364); insert into stu30 values(3,'sarbhjeet','chd','Harpal',78,30586); insert into stu30 values(4,'Ekjot','jal','Narinder',54,30754); insert into stu30 values(5,'palak','chd','Gurdev',75,59264); select * from stu30;
Output:R OL LN O
1 2 3 4 5
N AME
meenakshi meenakshi sarbhjeet Ekjot palak
ADDRES S
ldh jldhr chd jal chd
F ATHERNAME
Manish Manish Harpal Narinder Gurdev
MARKS
68 67 78 54 75
PH_NO
84736 87364 30586 30754 59264
2.Update rollno 2s name to Sandeep update stu30 set name='sandeep' where rollno=2; Output:R OL LN O
1 2 3 4 5
N AME
meenakshi sandeep sarbhjeet Ekjot palak
AD DRES S
ldh jldhr chd jal chd
F ATHERNAME
Manish Manish Harpal Narinder Gurdev
MARKS
68 67 78 54 75
PH_NO
84736 87364 30586 30754 59264
3.Update the ph_no of any student update stu30 set ph_no=48294 where rollno=2; Output:R OL LN O
1 2 3 4 5
N AME
meenakshi sandeep sarbhjeet Ekjot palak
ADDRES S
ldh jldhr chd jal chd
F ATHERNAME
Manish Manish Harpal Narinder Gurdev
MARKS
68 67 78 54 75
PH_NO
84736 48294 30586 30754 59264
4.Rollno and names of student whose marks are greater than 70. select rollno, name from stu30 where marks>70; Output:R OL LNO
3 5
NAME
sarbhjeet palak
N AME
meenakshi sandeep sarbhjeet Ekjot palak
ADDRES S
ldh jldhr chd jal chd
F ATHERNAME
manish manish Harpal Narinder Gurdev
MARKS
68 67 78 54 75
PH_NO
84736 48294 30586 30754 59264
6.Show the details of rollno =5 select * from stu30 where rollno=5; Output:R OL LN O
5
N AME
palak
AD DRES S
chd
F ATHERNAME
Gurdev
MARKS
75
PH_NO
59264
N AME
meenakshi sandeep sarbhjeet Ekjot
ADDRES S
ldh jldhr chd jal
F ATHERNAME
manish manish Harpal Narinder
MARKS
68 67 78 54
PH_NO
84736 48294 30586 30754
5) Create table student and execute following queries:1.show number of students whose marks >50% and marks <80% 2.calculate average marks,total marks 3.show names of student whose rollno between 1 to 10 and total marks are greater than 200 4.calculate minimum and maximum marks 5.show rno,marks,total marks and percentage 6. show marks of rollno 1,5 and 7. 7.Select name, rollno,marks, if marks are less than 50 than give 5 grace marks and then show result. Create table student (rno number(2),name varchar(10),eng number(3),math number(3),pbi number(3)); insert into student values(1,'meenakshi',75,46,68); insert into student values(2,'sandeep',92,67,87); insert into student values(3,'sarbhjeet',45,36,60); insert into student values(4,'ekjot',72,54,69); insert into student values(5,'geetika',52,48,75);
N AM E
meenakshi sandeep sarbhjeet ekjot geetika
ENG
75 92 45 72 52
MATH
46 67 36 54 48
PBI
68 87 60 69 75
1.Show number of students whose marks >50% and marks <80% Select name,(eng+math+pbi)/300*100 AS percentage from student where (eng+math+pbi)/300*100 BETWEEN 50 AND 80; Output:N AME
meenaksh i ekjot geetika
P ERCE NTAG E
63 65 58.33
2.Calculate average marks,total marks Select SUM(eng+math+pbi),SUM(eng+math+pbi)/3 AS average from student; Output:S UM (E NG+ MATH+P BI )
946
AV ERAG E
315.3
3.Show names of student whose rollno between 1 to 10 and total marks are greater than 200 Select rno,name,eng+math+pbi from student where rno between 1 and 10 and eng+math+pbi>200;
Output:R NO
2
NAME
sandeep
ENG+MATH+P BI
246
4.Calculate minimum and maximum marks Select MAX(eng+math+pbi),MIN(eng+math+pbi) from student; Output:M AX( EN G+ M ATH +P BI )
246
5.Show rno,marks,total marks and percentage Select rno,eng+math+pbi as total_marks,(eng+math+pbi)/300 *100 as percentage from student; Output:RNO
1 2 3 4 5
TOTAL_MARKS
189 246 141 195 175 63 82 47 65
PERCENTAGE
58.33
6. Show marks of rollno 1,5 and 7. select eng+math+pbi as marks from student where rno in(1,3,5); Output:MARKS
189 141 175
7.Select name, rollno,marks, if marks are less than 50 than give 5 grace marks and then show result. select name,rno,eng+math+pbi+5 as result from student where eng+pbi+math<150; Output:N AME
sarbhjeet 3
RNO
RES UL T
146
6).Create a table named emp with columns eno,ename,salary,dept,desg. Create table emp(eno number(2),ename varchar(10),salary number(10),dept number(5),desg varchar(10)); Table created Insert into emp values(1,aman,15000,10,clerk); 1 row(s) inserted. Insert into emp values(2,simran,25000,20,manager); 1 row(s) inserted. Insert into emp values(3,karampreet,12000,30,salesman); 1 row(s) inserted Insert into emp values(4,manjot,10000,30,salesman); 1 row(s) inserted Insert into emp values(5,gurpreet,17000,40,clerk); 1 row(s) inserted Select * from emp;
Output:E NO
1 2 3 4 5
EN AME
aman simran karampreet manjot gurpreet
S AL ARY
15000 25000 12000 10000 17000
DE PT
10 20 30 30 40
DE SG
clerk manager salesman salesman clerk
6).Create table dept with columns dno, dname, dptno , loc. create table dept(dno number(4), dname varchar2(20), dptno number(4), loc varchar2(10)); Table created insert into dept values(1,science,model town,ldh,10); 1 row(s) inserted. insert into dept values(2,computer,dholewal,chd,20); 1 row(s) inserted. insert into dept values(3,commerce,milar ganj,jld,30); 1 row(s) inserted. insert into dept values(4,englisg,arti chownk,ldh,40); 1 row(s) inserted. insert into dept values(5,punjabi,preet plc,chd,50); 1 row(s) inserted. select * from dept;
output:DNO
1 2 3 4 5
DNAME
science computer commerce englisg punjabi
LOC
model town dholewal milar ganj arti chowk preet plc
ADDRESS
ldh chd jld ldh chd
DPTNO
10 20 30 40 50
1. Put constraint foreign key on employee table column dno. alter table emp add(constraint fk_emp foreign key(dept) references dept(dno)); output:ORA-02270: no matching unique or primary key for this columnlist 2.Add constraint primary key in dept table column dno. alter table dept add( constraint pk_dep primary key(dno)); output:Table altered. 3.Make column loc of dept table that their should enter the values only ludhiana and jalandhar. alter table dept add(constraint ck_dep check(loc='ldh' or loc='jalandhar')); output:ORA-02293: cannot validate (SYSTEM.CK_DEP) - check constraint violated 7).list all employees whose ename starts with A and end with N. select * from emp where ename like 'a%n';
10
Output:E NO
1
E N AM E
aman
S AL ARY
15000
DE PT
10
DES G
clerk
8).list all employees whose designation is clerk or manager Select * from emp where desg= 'clerk' OR desg= 'manager'; Output:E NO
1 2 5 6
E N AM E
aman simran gurpreet gurnoor
S AL ARY
15000 25000 17000 8000
DE PT
10 20 40 10
DE SG
clerk manager clerk clerk
9). List all columns whose department other than 30 select * from emp where Dept NOT IN (30); Output:E NO
1 2 5 6
E N AM E
aman simran gurpreet gurnoor
S AL ARY
15000 25000 17000 8000
DE PT
10 20 40 10
DE SG
clerk manager clerk clerk
10). List ename,designation,salary of an employee of dept no 10 and whose salary is greater than 12000. Select ename,desg,salary from emp where dept=10 and salary>12000; Output:E N AM E
aman
DES G
clerk
S AL AR Y
15000
11).list all employees whose salary lies between 12500 and 30000 Select * from emp where salary BETWEEN 12500 and 30000;
11
Output:E NO
1 2 5
E N AM E
aman simran gurpreet
S AL ARY
15000 25000 17000
DE PT
10 20 40
DE SG
clerk manager clerk
12).List all employees who are not clerks Select ename,desg from emp where desg!= 'clerk'; Output:EN AME
simran
DE SG
manager
13).Select name of manager of department 20 whose salary is greater than 20,000. select ename,salary from emp where desg='manager' and dept=20 and salary>20000; Output:E N AM E
simran
S AL ARY
25000
14).Show the details of employees who is working as salesman and whose salary is greater than 11000. select * from emp where desg='salesman' and salary>11000; Output:E NO
3
EN AME
karampreet
S AL ARY
12000
DE PT
30
DE SG
salesman
12
15).Add new columns commission, basic_pay in table emp. alter table emp add(comm number(5),basic_pay number(5)); Output:Table altered update table emp set comm=500 where eno in(1,5); 2 row(s) updated update table emp set comm=200 where eno in(4,6,7); 3 row(s) updated update table emp set basic_pay=1000 where eno in(1,5,6); 3 row(s) updated update table emp set basic_pay=500 where eno in(3,7); 2 row(s) updated update table emp set basic_pay=2000 where eno in(8,9); 2 row(s) updated update table emp set basic_pay=1500 where eno=2; 1 row(s) updated select * from emp; Output:E NO
1 2 3 4 5 6 7 8 9
EN AME
aman simran karampreet manjot gurpreet gurnoor hardeep manjeet abhinav
S AL AR Y
15000 25000 12000 10000 17000 8000 11000 27000 15000
DE PT
10 20 30 30 40 10 30 50 60
DE SG
clerk manager salesman salesman clerk clerk salesman admin manager
CO MM
500 1000 200 200 500 200 200 -
BAS IC_P AY
1000 1500 500 500 1000 1000 500 2000 2000
B ASI C_ P AY
1000 1500
karampreet 500 manjot gurpreet gurnoor hardeep manjeet abhinav 500 1000 1000 500 2000 2000
17).Show the details of employees of department 10,20 and 30. select * from emp where dept in(10,20,30);
Output:E NO
1 2 3 4 6 7
EN AME
aman simran karampreet manjot gurnoor hardeep
S AL ARY
15000 25000 12000 10000 8000 11000
DE PT
10 20 30 30 10 30
DE SG
clerk manager salesman salesman clerk salesman
CO MM
500 1000 200 200 200 200
BAS IC_P AY
1000 1500 500 500 1000 500
18).Display ename,desg,basic_pay+500 as a bonus from table emp. select ename,desg,basic_pay+500 as bonus from emp;
14
Output:EN AME
aman simran
DE SG
clerk manager
BO NUS
1500 2000 1000 1000 1500 1500 1000 2500 2500
karampreet salesman manjot gurpreet gurnoor hardeep manjeet abhinav salesman clerk clerk salesman admin manager
19).List all employees who is not getting a commission. select ename,desg from emp where comm is NULL; Output:E N AM E
manjeet abhinav
DE SG
admin manager
20).List all employees who is getting a commission. select ename,desg from emp where comm is not NULL; Output:EN AME
aman simran
DE SG
clerk manager
karampreet salesman manjot gurpreet gurnoor hardeep salesman clerk clerk salesman
15
21). List all the names and enos as <eno> name is <ename>. select eno||' name is '||ename as employees from emp; Output:EMPLOYEES
1 name is aman 2 name is simran 3 name is karampreet 4 name is manjot 5 name is gurpreet 6 name is gurnoor 7 name is hardeep 8 name is manjeet 10 name is sandy 16 name is mandeep
22).Display names, total_salary of employees whose total_salary is greater than 20000. select ename,comm+basic_pay+salary as total_salary from emp where comm+salary+basic_pay>20000; Output:E N AM E
simran
TOT AL _S AL AR Y
27500
23).Show the details of students in ascending alphabetic order. select * from emp order by ename; Output:E NO
9 1 6 5 7 3 8 4 2
EN AME
abhinav aman gurnoor gurpreet hardeep karampreet manjeet manjot simran
S AL AR Y
15000 15000 8000 17000 11000 12000 27000 10000 25000
DE PT
60 10 10 40 30 30 50 30 20
DE SG
manager clerk clerk clerk salesman salesman admin salesman manager -
CO MM
500 200 500 200 200 200 1000
BAS IC_P AY
2000 1000 1000 1000 500 500 2000 500 1500
16
24). Names of employees whose bonus is between 10000 and 50000. select ename,(comm+salary)-basic_pay as bonus from emp where desg='manager' and (comm+salary)-basic_pay between 10000 and 50000; Output:E N AM E
simran
BONUS
24500
25). Show the names of employees whose names having only 5 characters only. select ename from emp where ename like '____'; Output:E NAME
aman
26).Count the number of designation titles in emp table. select COUNT(*),COUNT(desg) from emp; Output:C OU NT(*)
9 9
COUNT(DES G)
27).List the sum of sal and comm. of all employees where dept is 20. Select dept, SUM(salary),SUM(comm) from emp where dept=20; Output:S UM (S AL AR Y)
25000
SUM(COMM)
1000
28).Calculate sum of salaries of employees department wise. select dept,sum(salary) from emp group by dept; Output:17
D EP T
30 20 40 50 10 60
SUM(S AL ARY)
33000 25000 17000 27000 23000 15000
29).Count the number of employees in emp table under each desg title. select desg,count(desg) from emp group by desg; Output:DE SG
salesma n clerk manager admin 3 3 2 1
CO UNT(DE SG )
30).Calculate total number of employees under each desg title within each dept select desg,count(desg) from emp group by dept,desg; Output:DE SG
admin manager clerk salesma n clerk manager 1 1 2 3 1 1
CO UNT(DE SG )
31).Calculate sum of salaries group by dept using having clause. select dept,sum(salary) from emp group by dept having sum(salary)<20000;
18
Output:D EP T
40 60
SUM(S AL ARY)
17000 15000
32).Count number of clerk in deptno 10 select count(eno) from emp where desg='clerk' and dept=10; Output:COUNT(ENO)
2
33).Calculate total and average salary and count number of manager in deptno 20 select count(eno),sum(salary),avg(salary) from emp where desg='manager' and dept=20; Output:COUNT(ENO)
1
SUM(SAL ARY)
25000
AVG(SAL AR Y)
25000
34).Calculate max and min sal of clerks along with department. Select min(salary),max(salary),dept from emp where desg= 'clerk' group by dept; Output:MIN(SAL ARY)
17100 8100
MAX(SALARY)
17100 15100
DEPT
40 10
35). Select ename, desg, dname of each employee using equi join. select ename,desg,dname from emp,dept where emp.eno=dept.dno; Output:19
EN AME
aman simran
DE SG
clerk manager
DNAME
science computer commerce English Punjabi
36).Select ename, dept and dname where desg is clerk. select ename,emp.dept,dname from emp,dept where emp.dept=dept.dptno and desg='clerk'; Output:E N AME
gurnoor aman gurpreet
DEP T
10 10 40
DNAME
science science English
37).Select ename, desg, dptno and dname whose dptno is not 20 and 40. select ename,desg,dept.dptno,dname from emp,dept where emp.dept(+)=dept.dptno and dept.dptno not in(20,40); Output:EN AME
aman
DE SG
clerk
DP TNO
10 30 30 10 30 50
DNAME
science commerce commerce science commerce punjabi
karampreet salesman manjot gurnoor hardeep manjeet salesman clerk salesman admin
20
38).Select ename, dept, dname from emp and dept table using Cartesian join. select ename, emp.dept, dname from emp,dept order by ename; Output:E N AM E
abhinav abhinav abhinav abhinav abhinav aman aman aman aman aman
D EP T
60 60 60 60 60 10 10 10 10 10
DNAME
science commerce punjabi englisg computer science commerce computer englisg punjabi
39).Select eno,ename from emp table where either dept=10 or dept=30. select eno,ename from emp where dept=10 UNION select eno,ename from emp where dept=30; Output:E NO
1 3 4 6 7
ENAME
aman karampreet manjot gurnoor hardeep
40).Show eno and name of employees who work for deptno 10 and deptno 40. select eno,ename from emp where dept=10 MINUS select eno,ename from emp where dept=40; Output:21
E NO
1 6
E NAME
aman gurnoor
41).Show the names of employees whose name start with a, j or s using intersect operator. select eno,ename from emp where ename like 'a%' or ename like 's%' intersect select eno,ename from emp where ename like 'a%' or ename like 'j%'; Output:EN O
1 9
E NAME
aman abhinav
42).List department information of all department whose location is ldh. Select dname,dno from dept where loc = ldh; Output:No data found 43).List all employees who earn more than the average salary of all emplyees. select ename,dept,salary from emp where salary >(select AVG(salary) from emp e2 where emp.dept=emp.dept); Output:E N AM E
simran gurpreet
DEP T
20 40
S AL AR Y
25000 17000
22
manjeet
50
27000
44).List all employees whose name end with N. select eno,ename from emp where ename LIKE '%n'; Output:E NO
1 2
E NAME
aman simran
45).List number of employees of in science department table. select ename,salary,dname from EMP ,DEPT where dname='science' AND EMP.dept=DEPT.dptno; Output:ENAME
aman gurnoor
SALARY
15100 8100
DNAME
science science
46).List names of employees where sal>maximum sal of deptno=20. select ename,salary from EMP where salary>(select MAX(salary) from EMP where dept=20); Output:E N AM E
manjeet
S AL ARY
27000
47). List eno,ename,salary(incl. comm) and dname of all employees. select eno,ename,salary,salary+comm,dname from emp,dept where emp.dept=dept.dptno;
23
Output:E NO
1 2 3 4 5 6 7 8
EN AME
aman simran karampreet manjot gurpreet gurnoor hardeep manjeet
S AL ARY
15000 25000 12000 10000 17000 8000 11000 27000
S AL AR Y+ CO MM
15500 26000 12200 10200 17500 8200 11200 -
DNAME
science computer commerce commerce englisg science commerce punjabi
48).Display names of employees whose salary is greater than maximum salary of employees working in dept=20; select ename from emp where salary>(select max(salary) from emp where dept=20); Output:E N AM E
manjeet
49).Display all employees who are working in dept=10 and who earn atleast must as any employee in dept=30. select ename,salary,desg from emp where salary>= any(select salary from emp where dept=30) and dept=10; Output:E N AM E
aman
S AL ARY
15000
DE SG
clerk
50).Display all employees who are not in dept=30 and who earn more than all employees in dept=30. select ename,salary,desg from emp where salary>= all(select salary from emp where dept=30) and dept!=30;
24
Output:E N AM E
aman simran gurpreet manjeet abhinav
S AL ARY
15000 25000 17000 27000 15000
DES G
clerk manager clerk admin manager
51).Show name,salary,dept of the employee whose salary is greater than employee no 5 s salary. select eno,ename,salary,dept from emp where salary> (select salary from emp where eno=5); Output:ENO
2 8 16
ENAME
simran manjeet mandeep
SAL ARY
20000 20000 20000
DEPT
20 50 20
25
52)List all employees who do not work in the same department as that of its maneger. select * from emp where dept NOT IN (select dept from emp where desg='manager'); Output:EN O
1 3 4 5 6 7 8 10
DESG
clerk salesman salesman clerk clerk salesman admin clerk
COMM BASIC_P AY
500 200 200 500 200 200 1000 500 500 1000 1000 500 2000 -
53).Create view for clerks and perform insertion Create view clerk AS Select eno,ename,desg,dept from emp; Output:View created Insert into clerk(eno,ename,desg,dept) Values (10,'sandy','clerk',60); 54).Create a view by enabling read only option Create or replace view clerk AS Select eno,ename,desg,dept from emp where dept=20 and dept=40 with read only; 55).Create a join view Create or replace view emp_dept_veiw as
26
select emp.eno,emp.ename,emp.dept,dept.dname ,dept.loc from emp,dept where dept.loc IN('ldh','jld','chd'); 56). Create sequence with maximum value 50. Create SEQUENCE emp_dept Increment by 1 Start with 1 Maxvalue 50; Output Sequence created 57). Insertion in table emp using sequences Insert into emp_dept (eno,ename,salary) values (emp_dept.nextval, preet,21000); 58).Write a query to alter sequence emp_dept alter sequence emp_dept increment by 2 maxvalue 100; output sequence altered 59)Write a query to drop sequence emp_dept drop sequence emp_dept; output sequence dropped
27
60) Create a table STU with RNO(primary key), NAME(should start with S or A), AGE(should not be negative). CREATE TABLE stu (rno number(4) primary key, name varchar2(20) check(name like 'a%' or name like 's%'), age number(2) check(age>0)); output:Table created insert into stu values(1,'sehaj',22); 1 row(s) inserted insert into stu values(2,'aman',21); 1 row(s) inserted insert into stu values(3,'sandeep',22); 1 row(s) inserted insert into stu values(4,'simran',20); 1 row(s) inserted insert into stu values(5,'agamjeet',19); 1 row(s) inserted select * from stu; output:RNO
1 2 3 4 5
NAME
sehaj aman sandeep simran agamjeet
AGE
22 21 22 20 19
28
61) Display the name and age of students whose age is same as that of Sehaj. select name,age from stu where age in(select age from stu where name='sehaj'); output:NAME
sandee p sehaj
AGE
22 22
62) Show the details of student whose age is less than 20. select * from stu where age<20; output:RNO
5
NAME
agamjeet
AGE
19
63) Create a table STATUS with columns RNO, MARKS(both should not be negative), STATUS(pass, fail or absent). create table status (r_no number(4) constraint fk_status references stu(rno), marks number(4) check(marks>0), status varchar2(10) check(status in('pass','fail','absent'))); output:Table created insert into status values(2,45,'pass'); 1 row(s) inserted. insert into status values(4,65,'pass'); 1 row(s) inserted.
29
insert into status values(1,25,'fail'); 1 row(s) inserted. insert into status values(5,null,'absent'); 1 row(s) inserted. insert into status values(3,15,'fail'); 1 row(s) inserted. select * from status; output:R_NO
2 4 1 5 3
MARKS
45 65 25 15
STATUS
pass pass fail absent fail
64) Show the details of absenties. select * from status where status='absent'; output:R_NO
5 -
MARKS
STATUS
absent
65) Replace missing or Null values with not known. select r_no,status,NVL (to_char(marks),'not known') as marks from status; output:R_NO
2 4 1 5 3
STATUS
pass pass fail absent fail
MARKS
45 65 25 not known 15
30
PL/SQL PROGRAMS
--1Write a pl\sql program to find even number using if statement. declare n number := :n; begin if n mod 2 =0 then dbms_output.put_line(n || ' is even '); end if; end; Output 2 is even
--2write a pl\sql program to find number is positive using if statement. declare n number := :n; begin if n > 0 then dbms_output.put_line(n || ' is positive '); end if; end; Output 2 is positive
31
--3Write a pl\sql program to find number is positive or negative using if-else statement. declare n number := :n; begin if n > 0 then dbms_output.put_line(n || ' is positive '); else dbms_output.put_line(n || ' is negative '); end if; end; Output 2 is positive -5 is negative
--4Write a pl\sql program to find even or odd number using ifelse statement. declare n number := :n; begin if n mod 2 = 0 then dbms_output.put_line(n || ' is even '); else dbms_output.put_line(n || ' is odd'); end if; end; Output 2 is even
32
5 is odd --5Write a sql program to enter the days of a week according to numbers. declare n number := :n; begin if n = 1 then dbms_output.put_line('day is sunday'); elsif n = 2 then dbms_output.put_line('day is monday'); elsif n = 3 then dbms_output.put_line('day is tuesday'); elsif n = 4 then dbms_output.put_line('day is wednesday'); elsif n = 5 then dbms_output.put_line('day is thursday'); elsif n = 6 then dbms_output.put_line('day is friday'); elsif n = 1 then dbms_output.put_line('day is saturday'); else dbms_output.put_line('pls enter number b\w 1-7'); end if; end; Output day is sunday
33
--6Write a sql program to enter the colours according to numbers. declare c number := :c; begin if c = 1 then dbms_output.put_line('colour is green'); elsif c = 2 then dbms_output.put_line('colour is yellow'); elsif c = 3 then dbms_output.put_line('colour is sky'); elsif c = 4 then dbms_output.put_line('colour is red'); elsif c = 5 then dbms_output.put_line('colour is black'); elsif c = 6 then dbms_output.put_line('colour is blue'); elsif c = 7 then dbms_output.put_line('colour is pink'); else dbms_output.put_line('pls enter number b\w 1-7'); end if; end; Output colour is sky
34
--7Write a sql program to enter the months of the year according to numbers. declare m number := :m; begin if m = 1 then dbms_output.put_line('month is january'); elsif m = 2 then dbms_output.put_line('month is feburary'); elsif m = 3 then dbms_output.put_line('month is march'); elsif m = 4 then dbms_output.put_line('month is april'); elsif m = 5 then dbms_output.put_line('month is may'); elsif m = 6 then dbms_output.put_line('month is june'); elsif m = 7 then dbms_output.put_line('month is july'); elsif m = 8 then dbms_output.put_line('month is august'); elsif m = 9 then dbms_output.put_line('month is september'); elsif m = 10 then dbms_output.put_line('month is october'); elsif m = 11 then dbms_output.put_line('month is november'); elsif m = 12 then dbms_output.put_line('month is december'); else dbms_output.put_line('pls enter number b\w 1-12'); end if; end; Output month is november
35
--8Write a pl\sql program to find factorial of number. declare f number := 1; i number := 1; n number := :n; begin while i <= n loop f := f * i; i := i+1; end loop; dbms_output.put_line('factorial of ' || n || ' is ' || f); end; Output factorial of 4 is 24
Table of factorial create table factorial(num number(5),fact number(5)); Output Table created.
36
--9Write a pl\sql program to find factorial of number and inserting the corresponding values into table fact. declare f number := 1; i number := 1; n number := :n; begin while i <= n loop f := f * i; i := i+1; end loop; insert into factorial values(n,f); end; Output Statement processed.
37
--10 Write a pl\sql program to print counting from 1-10 using while loop. declare i number := 1; begin dbms_output.put_line('the counting is'); while i<=10 loop dbms_output.put_line(i); i := i+1; end loop; end; Output the counting is 1 2 3 4 5 6 7 8 9 10
38
--11Write pl\sql program to print table using for loop. declare n number := :n; begin dbms_output.put_line(' the table of '||n|| ' is '); for i in 1..10 loop dbms_output.put_line( n ||' * '|| i ||' = '|| n*i ); end loop; end; Output the table of 2 is 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20
39
--12 Write a pl\sql program to print counting from 10-1 using while loop. declare i number := 10; begin dbms_output.put_line('the counting is'); while i>=1 loop dbms_output.put_line(i); i := i-1; end loop; end; Output the counting is 10 9 8 7 6 5 4 3 2 1
40
--13Write a pl\sql program to print counting from 1-10 using for loop. begin dbms_output.put_line('the counting is'); for i in 1..10 loop dbms_output.put_line( i); end loop; end; Output the counting is 1 2 3 4 5 6 7 8 9 10
41
--14 Write a pl\sql program to print counting from 10-1 using for loop. begin dbms_output.put_line('the counting is'); for i in reverse 1..10 loop dbms_output.put_line( i); end loop; end; Output the counting is 10 9 8 7 6 5 4 3 2 1
42
--15To calculate simple interest. declare p number := :p; r number := :r; t number := :t; s number; begin s := (p*r*t)/100; dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '||r||',t = '||t||' is ' ||s); end; Output the simple interest of p = 5900,r = 2.5,t = 5 is 737.5
43
--16To calculate simple interest . --(1) if time is greater than 2yrs and less than 4yrs then rate is 8.5 --(2) if time is greater than 4yrs and less than 6yrs then rate is 5 --(3) if time is greater than 6yrs and less than 8yrs then rate is 3 --(4) else rate is 2 declare p number := :p; r number ; t number := :t; s number; begin if(t>2 and t<4)then r := 8.5; s := (p*r*t)/100; dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '|| r||',t = '||t||' is ' ||s); elsif(t>4 and t<6)then r := 5; s := (p*r*t)/100; dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '|| r||',t = '||t||' is ' ||s); elsif(t>6 and t<8)then r := 3; s := (p*r*t)/100; dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '|| r||',t = '||t||' is ' ||s); else r := 2;
44
s := (p*r*t)/100; dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '|| r||',t = '||t||' is ' ||s); end if; end; Output the simple interest of p = 5900,r = 2,t = 2 is 236
--17Write a pl\sql program to find sum of 10 even numbers. declare s number := 0; begin for i in 1..20 loop if i mod 2 = 0 then s := i + s; end if; end loop; dbms_output.put_line('sum of first 10 even numbers are ' || s); end; Output sum of
45
--18Write a pl\sql program to display message "welcome to pl\sql". begin dbms_output.put_line('welcome to pl\sql'); end; Output welcome to pl\sql
--19Write a pl\sql program to calculate product of two numbers. declare a number := :a; b number := :b; p number; begin p := a * b; dbms_output.put_line(' product of '||a ||' and '||b|| ' is '||p); end; Output product of 34 and 2 is 68
46
--20Write a pl\sql program to calculate division of two numbers. declare a number := :a; b number := :b; p number; begin p := a / b; dbms_output.put_line(' division of '||a ||' and '||b|| ' is '||p); end; Output division of 34 and 2 is 17
--21Write a pl\sql program to calculate addition of two numbers. declare a number := :a; b number := :b; p number; begin p := a + b; dbms_output.put_line(' addition of '||a ||' and '||b|| ' is '||p); end; Ouput addition of 34 and 2 is 36
47
--22Write a pl\sql program to calculate subtraction of two numbers. declare a number := :a; b number := :b; p number; begin p := a - b; dbms_output.put_line(' difference of '||a ||' and '||b|| ' is '||p); end; Output difference of 34 and 2 is 32
--23Write a pl\sql program to calculate area of circle. declare r number := :r; p number := 3.14; area number; begin area := p*r*r; dbms_output.put_line(' area of circle of '||r || ' is '||area); end; Output area of circle of 4 is 50.24
48
--24Write a pl\sql program to calculate area of rectangle. declare l number := :l; b number := :b; area number; begin area := l*b; dbms_output.put_line(' area of rectangle of '||l ||' and '||b|| ' is '|| area); end; Output area of rectangle of 6 and 8 is 48
--25Write a pl\sql program to calculate area of square. declare side number := :side; area number; begin area := 4*side; dbms_output.put_line(' area of square of '||side || ' is '||area); end; Output area of square of 4 is 16
49
--26Write a pl\sql program to calculate area of triangle. declare b number := :b; h number := :h; area number; begin area := 0.5*h*b; dbms_output.put_line(' area of triangle of '||b||' and ' ||h || ' is '|| area); end; Output area of triangle of 4.5 and 2 is 4.5
--27Write a pl\sql program to calculate telephone bill according to the number of calls made. declare c number := :c; cr number := :cr; t number ; begin t := c * cr; dbms_output.put_line(' total charges for calls made are '|| t); end; Output total charges for calls made are 90
50
--28Write a pl\sql program to find greater number among two. declare a number := :a; b number := :b; begin if a > b then dbms_output.put_line('a is greater'); else dbms_output.put_line('b is greater'); end if; end; Output b is greater
--29Write a pl\sql program to print the following pattern. --1 --2 2 --3 3 3 begin <<outer>> for i in 1..3 loop <<inner>> for j in 1..i loop dbms_output.put(i); end loop inner; dbms_output.new_line(); end loop outer; end;
51
Output 1 22 333
Table of emp1 create table emp1 (ename varchar2(20), sal number(5)); insert into emp1 values(:a, :b); select * from emp1; Output ENAME SAL Aman 1250 Bhumi 1500 3150 Sia 0
--30write a pl\sql program to print the ename of employee from table emp1 if salary of the given employee is greater than 3000 declare name emp.ename %type; begin select ename into name from emp1 where sal >3000; dbms_output.put_line(' name of employee is '|| name); end; Outout
52
name of employee is sia --31A program to display salary of an employee with specific empno from the emp table. declare salary emp.sal%type; ecode emp.eno%type; begin ecode := :ecode; select sal into salary from emp where eno = ecode; dbms_output.put_line('salary is'|| salary); end; Output salary is3200
--32A program to display the use of %rowtype attribute. declare employee emp%rowtype; begin employee.eno:=115; employee.ename :='anu'; insert into emp(eno,ename)values(employee.eno,employee.ename); dbms_output.put_line('row inserted'); end; Output row inserted select * from emp;
53
ENO ENAME SAL DEPT 101 baljeet 2000 10 5500 100 manpreet 10 0 2630 108 amanpreet 10 0 8950 105 kamal 20 2 7891 106 raman 20 0 4593 109 raju 30 0 113 geet 2500 50 114 jeet 2800 10 112 meet 1500 50 111 bhuwan 3200 40 103 muski 2000 30 121 aman 4000 50 115 anu -
--33Write a pl/sql program to print the information of a particular employee frim emp table using %rowtype. declare employee emp%rowtype; begin select * into employee from emp where eno=111; dbms_output.put_line(employee.ename); dbms_output.put_line(employee.sal); dbms_output.put_line(employee.dept); end; Output
54
bhuwan 3200 40
55
--34Write a program to insert a row into table. begin insert into emp(eno,ename,sal,dept)values(104,'ansh',1390,70); dbms_output.put_line('data entered'); end; Output data entered
--35Write a program in pl/sql to check wheather a number is even or odd if it is not equal to zero otherwise display the message number is zero. declare n integer; begin n := :n; if n<>0 then if n mod 2 = 0 then dbms_output.put_line(n||'is even'); else dbms_output.put_line(n||'is odd'); end if; else dbms_output.put_line('number is zero'); end if; end; Output 5 is odd
56
--36Write a program in pl/sql to print the grade of students when % of marks obtained is entered. --per>=80 - grade A --80>per>=60 - grade B --60>per>=45 - grade C --45>per - fail declare n number; begin --enter percent between 1 and 100 n := :n; if(n>=1 and n<=100) then if (n>=80) then dbms_output.put_line('grade A'); elsif(n>=60 and n<80) then dbms_output.put_line('grade B'); elsif(n>=45 and n<60) then dbms_output.put_line('grade C'); else dbms_output.put_line('fail'); end if; else dbms_output.put_line('percentage must between 01 and 100'); end if; end; Output grade C
57
--37Write a pl/sql program which executes a loop 3 times using exit statement. declare n number := 0; begin loop n := n+1 ; if(n>3)then exit; end if; dbms_output.put_line('loop executes'||n||'times'); end loop; end; Output loop executes1times loop executes2times loop executes3times
--38Write a pl/sql program which executes a loop 3 times using exit when statement. declare n number := 0; begin loop n := n+1 ; exit when n>3 ; dbms_output.put_line('loop executes'||n||'times'); end loop; end;
58
--39Write a pl/sql program to print the series of numbers starting from the numbers n till 1. declare n number :=:n; begin while n>=1 loop dbms_output.put_line(n); n :=n-1; end loop; end; Output 10 9 8 7 6 5 4 3 2 1
59
--40Write a program to print the values n to 1 in the same line. declare n number := :n; begin for i in reverse 1..n loop dbms_output.put_line(i); end loop; dbms_output.new_line; end; Output 6 5 4 3 2 1
--41Write a program in pl/sql to incremant the loop index by 5. begin for i in 1..50 loop if i mod 5=0 then dbms_output.put_line(i); end if; end loop; end;
60
Output 5 10 15 20 25 30 35 40 45 50
--42Write a program in pl/sql to show the importance of goto statement. begin dbms_output.put_line('first line'); goto third; dbms_output.put_line('second line'); <<third>> dbms_output.put_line('third line'); end; Output first line third line Table of emp22 create table emp22(eno number(5),sal number(5),commission number(4)); insert into emp22 values(:a,:b,:c);
61
select * from emp22; ENOSALCOMMISSIONENAME 100 10 500 aman 0 200 20 1000 mohit 0 400 30 1500 rohit 0 500 40 teena 0 600 50 meena 0
--43Create a user defined exceptions which checks whether the name of the employee is blank or not during insertion. declare ecode emp.eno%type; name emp.ename%type; ename_err exception; begin ecode :=:ecode; name :=:name; if name is null then raise ename_err; end if; insert into emp(eno,ename) values (ecode,name); dbms_output.put_line('data entered'); exception when ename_err ten
62
dbms_output.put_line('ename should not be blank'); end; Output ename should not be blank --44Program to fetch the department name and location of the given department number and if department number doesn't exist then program will raise the user-defined exception. declare location dept.loc%type; name dept.dname%type; dno dept.dno%type; deptno_err exception; begin dno := :dno; if dno not in (101,102,103,104) then raise deptno_err; end if ; select dname,loc into name, location from dept where dno=dno; dbms_output.put_line('location =' ||location); exception when deptno_err then dbms_output.put_line('deptno does not exist'); end; Output deptno does not exist
63
--45Program to use pragma exception_init. declare child_rec_present exception; pragma exception_init(child_rec_present,-2292); begin delete from emp where eno=102; dbms_output.put_line('data deleted'); exception when child_rec_present then dbms_output.put_line('first remove child records'); end; Output first remove child records
--46Program to use raise_application_error. declare commission emp.comm%type; ecode emp.eno%type; begin ecode := :ecode; select comm into commission from emp where eno=ecode; if commission is null then raise_application_error(-20991,'no commission'); else update emp set comm=comm *1.5 where eno=ecode; end if; exception when no_data_found then dbms_output.put_line('empno does not exist'); end;
64
--47Program to use sqlcode and sqlerrm in handler clauses. declare salary number; ecode emp.eno%type; begin ecode := :ecode; select sal into salary from emp where eno=ecode; dbms_output.put_line(salary); exception when OTHERS then dbms_output.put_line('error code ='||sqlcode); dbms_output.put_line('error message ='||sqlerrm); end; Output error code =100 error message =ORA-01403 no data found
65
--48Program of exception propogation. declare greater_sal exception; begin declare salary number; e number; begin e:= :e; select sal into salary from emp where eno=e; if salary>4000 then raise greater_sal; end if; dbms_output.put_line(salary); exception when no_data_found then dbms_output.put_line('eno does not exist'); end; exception when greater_sal then dbms_output.put_line('salary for employee greater than 4000'); end; Output salary for employee greater than 4000
66
--49Program to use exceptions raised in declarations. begin declare salary number(5,2) := 1500.50; begin dbms_output.put_line(salary); exception when OTHERS then dbms_output.put_line('wrong initialization'); end; exception when OTHERS then dbms_output.put_line('output block-wrong initialization'); end; Output output block-wrong initialization
67
--50Program to check whether record exist of the given employee number. if so, then retrieve his salary. declare salary number; begin select sal into salary from emp where eno= :eno; if sql%found then dbms_output.put_line('record is found'); dbms_output.put_line('salary is '|| salary); end if; exception when no_data_found then dbms_output.put_line('employee record is not present'); end; output record is found salary is 410
--51Program to use sql%notfound attribute to check whether the record is updated or not? begin update emp set sal=sal+100 where eno=:eno; if sql%notfound then dbms_output.put_line('record not found'); else dbms_output.put_line('record is updated'); end if; end; Output record not found
68
--52Program to count the number of records affected increasing the salaries of employees by 100 in emp table. begin update emp set sal= sal+100; dbms_output.put_line(sql%rowcount || 'rows updated'); end; Output 4rows updated --53Program to display the information of a given department number. declare cursor emp_data is select eno,ename,sal from emp where dno= :dno; ecode emp.eno%type; name emp.ename%type; salary emp.sal%type; begin open emp_data; loop fetch emp_data into ecode,name,salary; exit when emp_data%notfound; dbms_output.put_line(ecode || ' ' || name || ' ' || salary); end loop; close emp_data; end; Output 101 anu 4300 103 mani 6200
69
--54Program to increase the salary of the employees of the emp table such that manager salary is increased by 10%,clerk's salary is increased by 8% and 'salesman' salary by 5%? declare cursor sal_cursor is select eno,job from emp; post emp.job%type; ecode emp.eno%type; begin open sal_cursor; loop fetch sal_cursor into ecode,post; exit when sal_cursor%notfound; if post='manager' then update emp set sal=sal*1.10 where eno=ecode; elsif post='clerk' then update emp set sal=sal*1.08 where eno=ecode; elsif post='salesman' then update emp set sal=sal*1.05 where eno=ecode; end if; end loop; close sal_cursor; end; Output Statement processed.
70
--55Program to determines the top 4 employees with respect to salaries. declare cusor top_cursor is select ename,sal from emp order by sal desc; slary emp.sal%type; name emp.ename%type; n number; begin open top_cursor; if top_cursor%isopen then loop fetch top_cursor into name,salary; n:=top_cursor%rowcount; exit when n>4 or top_cursor%notfound; dbms_output.put_line(n || ' ' || name || ' ' || salary); end loop; end if; close top_cursor; end; Output 1 anu 2 mani 3 geet 4 gaurav 4640 6510 5170 5000
71
--56Program to calculate maximum,minimum and average salary department wise and display the results. declare cursor info_cursor is select dno,max(sal),min(sal),avg(sal) from emp group by dno; dcode emp.dno%type; maxsal emp.sal%type; minsal emp.sal%type; avgsal emp.sal%type; begin open info_cursor; loop fetch info_cursor into dcode,minsal,maxsal,avgsal; exit when info_cursor%notfound; dbms_output.put_line(dcode || ' ' || maxsal || ' ' || minsal || ' ' || avgsal); end loop; close info_cursor; end;
72
--57Program to display the department name and location using the single variable of % rowtype. declare cursor dept_cursor is select * from dept; d_record dept % rowtype; begin open dept_cursor; loop fetch dept_cursor into d_record; exit when dept_cursor%notfound; dbms_output.put_line(d_record.dname || ' ' || d_record.loc); end loop; close dept_cursor; end; Output purchase jalandher production chandigarh
73
--58Program to correct the gender code of all the employees. declare cursor g_cursor is select eno,gender from emp; gender12 emp.gender%type; ec emp.eno%type; begin open g_cursor; loop fetch g_cursor into ec,gender12; exit when g_cursor%notfound; if gender12 = 'M' then update emp set gender = 'F' where eno = ec; elsif gender12 = 'F' then update emp set gender = 'M' where eno = ec; end if; end loop; close g_cursor; end; Output 1 row(s) updated.
74
--59Program to print the department number and name of dept table using cursor for loop? declare cursor dept_cursor is select dno,dname from dept; begin for v in dept_cursor loop dbms_output.put_line(v.dno || ' ' || v.dname); end loop; end; Output 102 purchase 103 production
--60Program to pass parameters to cursors. declare cursor dept_cursor ( n number) is select eno,ename from emp where dno=n; name emp.ename%type; ecode emp.eno%type; num number; begin num := :num; open dept_cursor(num); loop fetch dept_cursor into ecode,name; exit when dept_cursor%notfound; dbms_output.put_line(ecode || ' ' || name); end loop; close dept_cursor; end;
75
--61Program to print department name and their corressponding employee information.Ensure that department name is not displayed with each record. declare cursor dep_cur is select dno,dname from dept; cursor emp_cur (dno number) is select * from emp where dno=dno; dcode dept.dno%type; name dept.dname%type; begin open dep_cur; loop fetch dep_cur into dcode,name; exit when dep_cur%notfound; dbms_output.put_line(name); dbms_output.put_line('..........'); for e in emp_cur (dcode) loop dbms_output.put_line(rpad(e.ename, 10) || ' ' || rpad(e.job, 10) || ' ' || e.sal); end loop; end loop; close dep_cur; end;
76
Output Purchase anu clerk 4644 geet manager 5170 mani salesman 6510 gaurav supervisor 5000 Production .......... anu clerk 4644 geet manager 5170 mani salesman 6510 gaurav supervisor 5000
77
--62Program to list the top n salaries and the employee namesto which they corresponds. declare cursor sal_cursor is select distinct sal from emp order by sal desc ; cursor emp_cursor(s number) is select ename from emp where sal=s; name emp.ename%type; salary emp.sal%type; n number; begin n := :n; open sal_cursor; loop fetch sal_cursor into salary; exit when sal_cursor%rowcount>n or sal_cursor%notfound; open emp_cursor(salary); loop fetch emp_cursor into name; exit when emp_cursor%notfound; dbms_output.put_line(rpad (name, 10) || ' ' || salary); end loop; close emp_cursor; end loop; close sal_cursor; end; Output mani geet gaurav anu
78
--63Program to list the records of employee from emp table within a given range of salaries. declare cursor emp_cursor (n1 number, n2 number) is select ename,sal from emp where sal>=n1 and sal<=n2; salary emp.sal%type; name emp.ename%type; m number; n number; begin m := :m; n := :n; open emp_cursor(m,n); loop fetch emp_cursor into name,salary; exit when emp_cursor%notfound; dbms_output.put_line(rpad (name, 10) || ' ' || salary); end loop; close emp_cursor; end; Output anu 4644 gaurav 5000 --64Create a trigger which will be fired for each insert or update on the ename,job column takes place on emp table. create or replace trigger emp_upper before insert or update of ename,job on emp for each row begin :new.ename := upper (:new.ename); :new.job :=upper (:new.job); end;
79
Output Trigger created insert into emp values (105,'amrit',6700,40,3003,'salesman','M'); Output 1 row(s) inserted.
--65Create a trigger which will be fired on deleting a record from the dept table. create or replace trigger dept_trig before delete on dept for each row begin delete from emp where dno = :old.dno; dbms_output.put_line(sql%rowcount || 'records are deleted from emp'); end; Output Trigger created. delete from dept where dno =102; Output records are deleted from emp 1 row(s) deleted.
80
--66Create a trigger that will automatically generate a primary key for a table. create or replace trigger emp_primary before insert on student for each row declare last_val number(4); new_val number(4); begin select nvl(max(rno),0) into last_val from student; new_val := last_val+1; :new.rno := new_val; end; Output Trigger created. insert into student values ('kriti',4,67); Output 1 row(s) inserted. --67Create a trigger which updates the salary of an employee and checks whether the new salary is greater then the old salary? create or replace trigger sal_check before update of sal on emp for each row begin if(:new.sal <= :old.sal)then raise_application_error(-20004,'new salary must be greater than previous'); end if; end;
81
Output Trigger created. update emp set sal=8000 where eno=102; Output 1 row(s) updated.
--68Create a trigger that updates the values of the corressponding rows in the emp table whenever the value of dno changes in the dept table. create or replace trigger casdcade_upd after update of dno on dept for each row begin update emp set emp.dno =:new.dno where emp.dno =:old.dno; end; Output Trigger created. update dept set dno=105 where dname='production'; Output 1 row(s) updated.
82
--69Create a trigger which illustrates the operation performed by a user on emp table? create or replace trigger opr_type before insert or update or delete on emp begin if inserting then dbms_output.put_line('insert operation is performed'); elsif updating then dbms_output.put_line('update operation is performed'); else dbms_output.put_line('delete operation is performed'); end if; end; Output Trigger created. delete from emp where eno= 102; Output delete operation is performed 1 row(s) deleted.
83