SQL Queries Used in SSMP (Scholarship and Stipend Management Portal)
Database and Schema Creation
Create Database
CREATE DATABASE IF NOT EXISTS ssmp;
USE ssmp;
Create Departments Table
CREATE TABLE IF NOT EXISTS departments ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) UNIQUE NOT NULL, faculty VARCHAR(100) NOT NULL, budget FLOAT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT dept_budget_check CHECK (budget >= 0));
Create Admins Table
CREATE TABLE IF NOT EXISTS admins ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, dept_id INT NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (dept_id) REFERENCES departments(id), CONSTRAINT admin_email_check CHECK (email LIKE '%@bup.edu.bd'));
Create Students Table
CREATE TABLE IF NOT EXISTS students ( student_id BIGINT PRIMARY KEY, reg_no BIGINT UNIQUE NOT NULL, dept_id INT NOT NULL, name VARCHAR(100) NOT NULL, session VARCHAR(20) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (dept_id) REFERENCES departments(id), CONSTRAINT student_email_check CHECK (email LIKE '%@%'));
CREATE TABLE IF NOT EXISTS income_records ( id INT PRIMARY KEY AUTO_INCREMENT, student_id BIGINT NOT NULL, amount FLOAT NOT NULL, source VARCHAR(255) NOT NULL, family_member INT NOT NULL, date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE CASCADE, CONSTRAINT income_amount_check CHECK (amount >= 0), CONSTRAINT family_member_check CHECK (family_member >= 0));
Create Applications Table
CREATE TABLE IF NOT EXISTS applications ( id INT PRIMARY KEY AUTO_INCREMENT, student_id BIGINT NOT NULL, type VARCHAR(255) NOT NULL, semester VARCHAR(50) NOT NULL, status VARCHAR(100) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (student_id) REFERENCES students(student_id));
Create Scholarships Table
CREATE TABLE IF NOT EXISTS scholarships ( id INT PRIMARY KEY AUTO_INCREMENT, student_id BIGINT NOT NULL, student_name VARCHAR(100) NOT NULL, type VARCHAR(100) NOT NULL, amount FLOAT NOT NULL, semester VARCHAR(50) NOT NULL, awarded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (student_id) REFERENCES students(student_id));
Create Stipends Table
CREATE TABLE IF NOT EXISTS stipends ( id INT PRIMARY KEY AUTO_INCREMENT, student_id BIGINT NOT NULL, student_name VARCHAR(100) NOT NULL, type VARCHAR(100) NOT NULL, amount FLOAT NOT NULL, semester VARCHAR(50) NOT NULL, awarded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (student_id) REFERENCES students(student_id));
SELECT Queries
Authentication Queries
Find Admin by Email
SELECT * FROM admins WHERE email = ?;
Find Student by Email
SELECT * FROM students WHERE email = ?;
Find Student by Student ID
SELECT * FROM students WHERE student_id = ?;
Get User by ID (Admin)
SELECT * FROM admins WHERE id = ?;
Get User by ID (Student)
SELECT * FROM students WHERE student_id = ?;
Department Queries
Get Department by ID
SELECT * FROM departments WHERE id = ?;
Student Queries
Get Students by Department
SELECT * FROM students WHERE dept_id = ?;
Search Students with Filters
SELECT * FROM studentsWHERE dept_id = ?AND ( name LIKE ? OR email LIKE ? OR student_id LIKE ? OR reg_no LIKE ? OR session LIKE ?);
Get Student by Student ID
SELECT * FROM students WHERE student_id = ?;
Academic Record Queries
Get Academic Record by Student ID
SELECT * FROM academic_records WHERE student_id = ?;
Get Academic Record by Registration Number
SELECT * FROM academic_records WHERE reg_no = ?;
Get Academic Records for Department (with Join)
SELECT academic_records.*FROM academic_recordsJOIN students ON academic_records.student_id = students.student_idWHERE students.dept_id = ?;
Scholarship Queries
Get Scholarships by Student ID
SELECT * FROM scholarshipsWHERE student_id = ?ORDER BY awarded_at DESC;
Get Scholarship by Student and Semester
SELECT * FROM scholarshipsWHERE student_id = ? AND semester = ?;
Get Scholarship by Student, Type and Semester
SELECT * FROM scholarshipsWHERE student_id = ? AND type = ? AND semester = ?;
Get All Scholarships for Department (with Student Join)
SELECT scholarships.*, students.*FROM scholarshipsJOIN students ON scholarships.student_id = students.student_idWHERE students.dept_id = ?ORDER BY scholarships.awarded_at DESC;
Stipend Queries
Get Stipends by Student ID
SELECT * FROM stipendsWHERE student_id = ?ORDER BY awarded_at DESC;
Get Stipend by Student and Semester
SELECT * FROM stipendsWHERE student_id = ? AND semester = ?;
Get Stipend by Student, Type and Semester
SELECT * FROM stipendsWHERE student_id = ? AND type = ? AND semester = ?;
Get All Stipends for Department (with Student Join)
SELECT stipends.*, students.*FROM stipendsJOIN students ON stipends.student_id = students.student_idWHERE students.dept_id = ?ORDER BY stipends.awarded_at DESC;
Application Queries
Get Applications by Student ID
SELECT * FROM applicationsWHERE student_id = ?ORDER BY created_at DESC;
Get Pending Application by Student, Type and Semester
SELECT * FROM applicationsWHERE student_id = ? AND type = ? AND semester = ? AND status = 'Pending';
Get All Applications for Department (with Student Join)
SELECT applications.*FROM applicationsJOIN students ON applications.student_id = students.student_idWHERE students.dept_id = ?;
Get Application by ID
SELECT * FROM applications WHERE id = ?;
Income Record Queries
Get Income Record by Student ID (Latest)
SELECT * FROM income_recordsWHERE student_id = ?ORDER BY date DESCLIMIT 1;
Get Income Record by Student ID
SELECT * FROM income_records WHERE student_id = ?;
Join Queries for Scholarship Eligibility
Get Students with Academic Records for Scholarship Evaluation
Delete Student (Cascade deletes academic_records and income_records)
DELETE FROM students WHERE student_id = ?;
Delete Scholarship
DELETE FROM scholarships WHERE id = ?;
Delete Stipend
DELETE FROM stipends WHERE id = ?;
Delete Application
DELETE FROM applications WHERE id = ?;
Aggregate and Join Queries
Count Total Students in Department
SELECT COUNT(*) FROM students WHERE dept_id = ?;
Sum of All Scholarships for Department
SELECT SUM(scholarships.amount)FROM scholarshipsJOIN students ON scholarships.student_id = students.student_idWHERE students.dept_id = ?;
Sum of All Stipends for Department
SELECT SUM(stipends.amount)FROM stipendsJOIN students ON stipends.student_id = students.student_idWHERE students.dept_id = ?;
Count of Scholarships Awarded in Department
SELECT COUNT(*)FROM scholarshipsJOIN students ON scholarships.student_id = students.student_idWHERE students.dept_id = ?;
Count of Stipends Awarded in Department
SELECT COUNT(*)FROM stipendsJOIN students ON stipends.student_id = students.student_idWHERE students.dept_id = ?;
Get Academic Records Statistics for Department
SELECT academic_records.*FROM academic_recordsJOIN students ON academic_records.student_id = students.student_idWHERE students.dept_id = ?;
Get Students with Academic Records and Income Information
SELECT students.*, academic_records.*, income_records.*FROM studentsLEFT JOIN academic_records ON students.student_id = academic_records.student_idLEFT JOIN income_records ON students.student_id = income_records.student_idWHERE students.dept_id = ?;
Get Application Details with Student Information
SELECT applications.*, students.name, students.email, students.sessionFROM applicationsJOIN students ON applications.student_id = students.student_idWHERE applications.id = ?;
Get Scholarship Distribution by Type
SELECT type, COUNT(*) as count, SUM(amount) as total_amountFROM scholarshipsJOIN students ON scholarships.student_id = students.student_idWHERE students.dept_id = ?GROUP BY type;
Get Stipend Distribution by Type
SELECT type, COUNT(*) as count, SUM(amount) as total_amountFROM stipendsJOIN students ON stipends.student_id = students.student_idWHERE students.dept_id = ?GROUP BY type;
Get Average CGPA for Department
SELECT AVG(academic_records.cgpa)FROM academic_recordsJOIN students ON academic_records.student_id = students.student_idWHERE students.dept_id = ?;
Get Students Above Certain CGPA Threshold
SELECT students.*, academic_records.cgpaFROM studentsJOIN academic_records ON students.student_id = academic_records.student_idWHERE students.dept_id = ? AND academic_records.cgpa >= ?;
Get Total Financial Aid per Student
SELECT students.student_id, students.name, COALESCE(SUM(scholarships.amount), 0) as scholarship_total, COALESCE(SUM(stipends.amount), 0) as stipend_total, COALESCE(SUM(scholarships.amount), 0) + COALESCE(SUM(stipends.amount), 0) as total_aidFROM studentsLEFT JOIN scholarships ON students.student_id = scholarships.student_idLEFT JOIN stipends ON students.student_id = stipends.student_idWHERE students.dept_id = ?GROUP BY students.student_id, students.name;