Consider following table Student (rollno (P.K.), name, percentage) Write a PL/SQL code to display top 10 records of student along with their class. (Class is calculated from percentage).

First We create table and enter some random data
create table student
(
roll_no number primary key,
name varchar2(20) not null,
percentage number
);

insert into student values(101,'Rahul',78);
insert into student values(102,'Riya',31);
insert into student values(103,'Kamlesh',82);
insert into student values(104,'Dhoni',98);
insert into student values(105,'Mitali',65);
insert into student values(106,'Jayesh',16);
insert into student values(107,'Hansa Ben',91);
insert into student values(108,'Bhupendra Jogi',75);
insert into student values(109,'Elvis',15);
insert into student values(110,'Modi Kaka',99);
declare 
	class varchar2(20);
	cnt number:=1;
	cursor c1 is 
	select * 
	from student 
	order by percentage desc;
	
begin 
	dbms_output.put_line('-----------------------------------------------------------');
	dbms_output.put_line('TOP 5 STUDENT in Exam');
	dbms_output.put_line('------------------------------------------------------------');
	dbms_output.put_line('STUDENT NAME        Percentage        Class');
	dbms_output.put_line('------------------------------------------------------------');
	for x in c1 
	loop 
		exit when cnt>5;
		if x.percentage >=90 then 
			class:='First with distinct';
		elsif x.percentage >=80 then 
			class:='First class';
		elsif x.percentage >=70 then 
			class:='Second class';
		elsif x.percentage >=60 then 
			class:='Pass class';
		else 
			class:='Fail';
		end if;
		dbms_output.put_line(rpad(x.name,25) || ' ' || rpad(x.percentage,15)  || class);
		cnt:=cnt+1;
	end loop;
	dbms_output.put_line('--------------------------------------------------------------');
end;
/



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *