Skip to content

Instantly share code, notes, and snippets.

@rithask
Last active November 14, 2023 02:00
Show Gist options
  • Save rithask/1f09fe525c8fbb2e660048786b2843c8 to your computer and use it in GitHub Desktop.
Save rithask/1f09fe525c8fbb2e660048786b2843c8 to your computer and use it in GitHub Desktop.
KTU S5 DBMS
/*
AIM:
Create a cursor that stores the details of students eligible for honours in roll number order. Assume that semester grades of s1 and s2 are stored in students table along with Roll number and name of students. A student is eligible for honors if the total grade for s1 and s2 is greater than 12. Print the highest grade obtained in the previous semesters for honors students using the cursor created.
*/
-- Create the table to store the student details.
CREATE TABLE students (
roll_number NUMBER,
name VARCHAR2(20),
s1 NUMBER,
s2 NUMBER
);
-- Insert sample data.
INSERT INTO students VALUES (1, 'A', 7.5, 7);
INSERT INTO students VALUES (2, 'B', 8, 9.5);
INSERT INTO students VALUES (3, 'C', 9, 9);
INSERT INTO students VALUES (4, 'D', 8.8, 9);
-- Declare variables to store student details and highest grade.
DECLARE
v_roll_number students.roll_number%TYPE;
v_name students.name%TYPE;
v_s1 students.s1%TYPE;
v_s2 students.s2%TYPE;
v_total_grade NUMBER;
v_highest_grade NUMBER := 0;
-- Cursor to fetch details of honors students.
CURSOR honors_students_cursor IS
SELECT roll_number, name, s1, s2
FROM students
WHERE s1 + s2 > 12
ORDER BY roll_number;
BEGIN
-- Open the cursor.
OPEN honors_students_cursor;
-- Fetch and process each honors student.
LOOP
FETCH honors_students_cursor INTO v_roll_number, v_name, v_s1, v_s2;
EXIT WHEN honors_students_cursor%NOTFOUND;
-- Calculate the total grade for the current student.
v_total_grade := v_s1 + v_s2;
-- Print student details.
DBMS_OUTPUT.PUT_LINE('Roll Number: ' || v_roll_number);
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Total Grade (S1 + S2): ' || v_total_grade);
-- Update the highest grade if needed.
IF v_total_grade > v_highest_grade THEN
v_highest_grade := v_total_grade;
END IF;
END LOOP;
-- Close the cursor.
CLOSE honors_students_cursor;
-- Print the highest grade obtained by honors students.
DBMS_OUTPUT.PUT_LINE('Highest Grade Obtained by Honors Students: ' || v_highest_grade);
END;
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment