Skip to content

Instantly share code, notes, and snippets.

@rithask
Last active October 25, 2023 02:49
Show Gist options
  • Save rithask/78f3349a8bd932b764c11c4e17bf50ef to your computer and use it in GitHub Desktop.
Save rithask/78f3349a8bd932b764c11c4e17bf50ef to your computer and use it in GitHub Desktop.
PL/SQL program to find average mark of students
/*
AIM:
Write a pl/sql block to find average mark of students in a class and print the marks;Print a message " Need improvement " if the class average is less than 40. At the bottom print today's date and day of the week (monday, tuesday etc.)
*/
DECLARE
-- Declare variables to store class average and student marks.
class_average NUMBER := 0;
total_students NUMBER := 0;
student_average NUMBER := 0;
-- Cursor to fetch student marks.
CURSOR student_marks IS
SELECT student_id, mark1, mark2, mark3
FROM Student;
BEGIN
DBMS_OUTPUT.PUT_LINE('ID' || chr(09) || 'Average Mark');
-- Loop through the student records.
FOR student_rec IN student_marks LOOP
-- Calculate the average for each student.
student_average := (student_rec.mark1 + student_rec.mark2 + student_rec.mark3) / 3;
-- Print student marks.
DBMS_OUTPUT.PUT(student_rec.student_id || chr(09));
DBMS_OUTPUT.PUT(student_average || chr(09));
-- Check if the student needs improvement and print the message.
IF student_average < 40 THEN
DBMS_OUTPUT.PUT('Need improvement');
END IF;
DBMS_OUTPUT.NEW_LINE;
-- Update class average and count.
class_average := class_average + student_average;
total_students := total_students + 1;
END LOOP;
-- Calculate the class average.
IF total_students > 0 THEN
class_average := class_average / total_students;
-- Print the class average.
DBMS_OUTPUT.PUT_LINE('Class Average: ' || class_average);
-- Check if the class average is less than 40 and print the message.
IF class_average < 40 THEN
DBMS_OUTPUT.PUT_LINE('Class Needs improvement');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('No students found.');
END IF;
-- Print the current date and day of the week.
DBMS_OUTPUT.PUT_LINE('Current Date: ' || TO_CHAR(SYSDATE, 'MM/DD/YYYY'));
DBMS_OUTPUT.PUT_LINE('Day of the Week: ' || TO_CHAR(SYSDATE, 'DAY'));
END;
/
-- Student table
CREATE TABLE Student (
student_id NUMBER PRIMARY KEY,
student_name VARCHAR2(20),
mark1 NUMBER,
mark2 NUMBER,
mark3 NUMBER
);
-- Insert data into the Student table
INSERT INTO Student VALUES (1, 'A', 50, 60, 70);
INSERT INTO Student VALUES (2, 'B', 40, 50, 60);
INSERT INTO Student VALUES (3, 'C', 30, 40, 50);
INSERT INTO Student VALUES (4, 'D', 20, 30, 40);
-- Output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment