This project simulates a smart university academic analyzer within a PostgreSQL database. It uses advanced PL/pgSQL functions, views, and window functions to generate detailed student transcripts, calculate semester and cumulative GPAs, and provide analytical views based on academic performance.
This project was developed from a series of "Mathematical Databases" course homework assignments to create a single, cohesive, and self-contained application.
- Accurate Grade Point Conversion: A standardized function converts letter grades (e.g., 'A', 'B+', 'C-') to a standard numerical 4.0 scale (e.g., 4.0, 3.3, 1.7), correctly handling non-GPA-applicable grades.
- Transcript Generation Function: A
udf_generate_transcript(student_id)function returns a complete academic history for a student, grouped by term, showing all courses, credits, grades, semester GPA, and a running cumulative GPA. - Credit-Weighted GPA Logic: All GPA calculations are correctly weighted by course credits and properly handle
NULLor non-standard grades. - Student Summary View: A
v_student_overall_summaryview provides a high-level look at each student's total earned credits and final cumulative GPA. - Department Performance View: A
v_department_performanceview calculates the true, credit-weighted average GPA for all students in each department. - Dean's List Function: A
udf_get_deans_list(semester, year, min_gpa)function returns students who meet a specific GPA threshold for a given term.
- Advanced SQL: Extensive use of multi-table joins, aggregations (
SUM,AVG,COUNT), and complexCASEstatements. - Window Functions: The core of the project. Used to calculate running totals for cumulative GPA (
SUM(...) OVER (PARTITION BY ... ORDER BY ...)). - PL/pgSQL: Writing table-returning functions (
RETURNS TABLE) to create a clean, parameterized API for reports. - Database Views: Creating a layered system of views (
v_student_grade_points->v_student_academic_summary->v_student_overall_summary) to abstract complexity and promote reusability. - Data Transformation: Converting data from one format to another (letter grades to numeric points) and aggregating transactional data (
takes) into meaningful reports. - Reporting & Analytics: Generating meaningful summaries (transcripts, departmental performance) from raw data.
The system is built on two pillars: the database schema (the tables) and the business logic (the functions and views).
This project builds on the existing university schema. The logic primarily revolves around these key tables:
student: Stores student information (ID, name, department).course: Stores course definitions, includingcourse_id,title, andcredits.takes: The central transaction table. An entry here signifies that astudenthas taken asection. It stores thegrade.department: Used for grouping students to calculate departmental performance.
The logic is implemented in a series of layered views and functions.
udf_convert_grade_to_points(grade): A simple, immutable function that converts a letter gradeVARCHARinto aNUMERICgrade point. This is the single source of truth for all grade conversions.v_student_grade_points: This is the foundational view. It joinstakesandcourseand, for every row, calculates thegrade_points(from the function above) and thequality_points(which isgrade_points * credits). It also includes boolean flagsis_gpa_applicableandis_passedfor precise calculations.
v_student_academic_summary: This is the main engine for the transcript. It first calculates per-semester stats (total quality points and GPA-applicable credits). Then, it uses window functions to calculate the running cumulative quality points and credits. Finally, it joins this data back to the per-course view (v_student_grade_points) to create a complete row-by-row academic history, annotated with the correctsemester_gpaandcumulative_gpafor that term.v_student_overall_summary: This view provides a simple, one-row-per-student summary. It calculates the finalcumulative_gpaand thetotal_credits_earned(based onis_passed = true).v_department_performance: This view calculates a true, credit-weighted average GPA for each department by aggregating thequality_pointsandcreditsfrom all students in that department.
udf_generate_transcript(student_id): This is a simple SQL function that acts as a clean API. It justSELECTs all relevant columns from thev_student_academic_summaryview for a specific student, providing a ready-to-use transcript.udf_get_deans_list(semester, year, min_gpa): This function usesDISTINCT ON (id, semester, year)on the main summary view to get a single, definitive GPA for each student for each term, then filters them by the function parameters.
postgres-academic-performance-analyzer/
├── .gitignore # Ignores system and credential files
├── LICENSE # MIT license file
├── README.md # This documentation
├── demo.sql # Example script showing how to use the system
└── src/
├── 00_schema.sql # Main DDL script to create all tables
├── 01_data.sql # Script to insert sample data
├── 02_grade_helpers.sql # Creates grade conversion function and base view
├── 03_core_views.sql # Creates the main analytical/summary views
└── 04_transcript_functions.sql # Creates the final report functions
-
Setup Database: Create a new PostgreSQL database.
createdb university_db
-
Connect to Database: Use
psqlor any SQL client to connect to your new database.psql university_db
-
Run SQL Scripts (in order): Execute the SQL scripts from the
src/directory in numerical order.-- 1. Create the schema \i src/00_schema.sql -- 2. Load the sample data \i src/01_data.sql -- 3. Create helper views and functions \i src/02_grade_helpers.sql -- 4. Create the main analytical views \i src/03_core_views.sql -- 5. Create the final report functions \i src/04_transcript_functions.sql
-
Test the System: Run the
demo.sqlscript to see the system in action. This script is wrapped in aBEGIN...ROLLBACKblock, so it will not make permanent changes to your data.\i demo.sql
You will see output for each test case, for example:
student_id | name | dept_name | total_credits_earned | cumulative_gpa ------------+---------+-------------+----------------------+---------------------- 00128 | Zhang | Comp. Sci. | 7 | 3.8571428571428571 12345 | Shankar | Comp. Sci. | 14 | 3.5000000000000000 45678 | Levy | Physics | 7 | 2.5454545454545455 (3 rows)
You can call the functions directly at any time:
-- Generate a transcript for a specific student
SELECT * FROM udf_generate_transcript('12345');
-- Find students for the Dean's List
SELECT * FROM udf_get_deans_list('Fall', 2017, 3.75);Feel free to connect or reach out if you have any questions!
- Maryam Rezaee
- GitHub: @msmrexe
- Email: [email protected]
This project is licensed under the MIT License. See the LICENSE file for full details.