Jun 03, 2021 8:48 am
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
Jun 03, 2021 9:11 am
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; /
Dec 29, 2021 4:08 pm
Nice. Let us look for other approaches as well.