Forum

Write a PL/SQL code...
 
Share:
Notifications
Clear all

Write a PL/SQL code to assign GRADE to every student based on their marks according to the following rules.


Posts: 47
Guest
Topic starter
(@Priya Roy)
Eminent Member
Joined: 3 years ago

Create a table for Student (Sid, Marks, grade). Insert 5 records containing values of Sid and Marks. Write PL/SQL code to assign grades to every student based on their marks according to the following rules.

90 and above : O
80 to 89 : E
70 to 79 : A
60 to 69 : B
Otherwise: C

How Can we achieve this? 

2 Replies
Posts: 18
(@jeevan303500)
Active Member
Joined: 2 years ago

You can consider the student table for this example. Assume that It has three columns Sid, Marks and grade 

Create table Student 
(
Sid integer primary key,
 marks integer, 
grade char(2)
);

 Now I am inserting values into this student table.

insert into Student(Sid, marks) VALUES(1,80);
insert into Student(Sid, marks) VALUES(2,90);
insert into Student(Sid, marks) VALUES(3,70);
insert into Student(Sid, marks) VALUES(4,67);
insert into Student(Sid, marks) VALUES(5,79);

The below PLSQL procedure will you the expected output.

declare
    
       noOfRows integer;
       studMarks integer;
       loopVar integer :=1;
begin
       select count(*) into noOfRows from Student;
       while loop Var <=noOfRows
       loop
               select marks into studMarks from student where sid=loopVar;
                if studMarks>=90 then
                          update student set grade ='O' where sid=loopVar;
                elseif studMarks>=80 and studMarks<=89 then
                          update student set grade = 'E' where sid=loopVar;
                elseif studMarks>=70 and studMarks<=79 then
                          update student set grade = 'A' where sid=loopVar;
                elseif studMarks>=60 and studMarks<=69 then
                          update student set grade='B' where sid=loopVar;
                else
                          update student set grade = 'C' where sid=loopVar;
                end if;
                loopVar :=loopVar+1;
                end loop;
                end;
                 /
Reply

Self Learning Video Tutorials - Software Course

Posts: 10
Admin
(@sql-admin)
Active Member
Joined: 3 years ago

Nice. Let us look for other approaches as well.

Reply

Leave a reply

Author Name

Author Email

Title *

Preview 0 Revisions Saved
Share: