# Forum

Write a PL/SQL code...

Share:
Clear all

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

3 Posts
3 Users
0 Likes
3,987 Views
Posts: 47
Priya Roy
Guest
Topic starter
(@Priya Roy)
Eminent Member
Joined: 2 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: 1 year 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,
);
```

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;
/```
Posts: 8
Active Member
Joined: 2 years ago

Nice. Let us look for other approaches as well.