Student Allotment SQL Problem
Student Allotment SQL Problem
A college needs to develop a system to allocate Open Elective Subjects to its respective students. The way the
system would work is that each student is allowed 5 choices with the respective preference, where number 1
indicates the first preference, number 2 indicates second preference and so on, the subjects are supposed to be
allotted on the basis of the Student’s GPA, which means the student with the students with the highest GPAs
are allotted the subject they want. Every subject has a limited number of seats so if a subject has 60 seats and
all of them are filled then the student would not be allotted his first preference but instead second would be
checked, if the second preference is full as well then the third preference would be checked, this process would
be repeated till the student is allotted a subject of his/her choice. If in case all the preferences that the student
has selected are already full, then the student would be considered as unallotted and would be marked so.
For example, Mohit has filled his 5 choices with the respective preferences and they are as following:
The below table has the subject to student mapping with the preference
The below table has the details of subjects such as Subject Id, Subject name, and the maximum number of seats
SubjectId StudentId
PO1491 159103036
(Table Name: Allotments)
StudentId
159103036
(Table Name: UnallotedStudents)
Your Task is to write a Stored Procedure to assign all the students to a respective subject according the above
stated workflow
.
Database Schema
Storage Procedure
CREATE PROCEDURE AllocateSubjects()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE student_id INT;
DECLARE cur CURSOR FOR
SELECT StudentId
FROM StudentDetails
ORDER BY GPA DESC;
OPEN cur;
read_loop: LOOP
FETCH cur INTO student_id;
IF done THEN
LEAVE read_loop;
END IF;
CLOSE cur;
END;