-- Comprehensive SQL Queries for Student Management System
---
-- 1. Basic Queries
SELECT * FROM Students; -- Retrieve all students
SELECT * FROM Courses; -- Retrieve all courses
SELECT * FROM Teachers; -- Retrieve all teachers
SELECT * FROM Grades; -- Retrieve all grades
---
-- 2. Filtering Data
SELECT * FROM Students WHERE department_id = 101; -- Students in a specific
department
SELECT * FROM Teachers WHERE qualification = 'PhD'; -- Teachers with a specific
qualification
SELECT * FROM Courses WHERE semester = 5; -- Courses offered in a specific semester
---
-- 3. Sorting Data
SELECT * FROM Students ORDER BY student_name ASC; -- List students sorted by their
name
SELECT * FROM Courses ORDER BY credits DESC; -- List courses sorted by credits in
descending order
---
-- 4. Aggregate Functions
SELECT COUNT(*) AS total_students FROM Students; -- Total number of students
SELECT course_id, AVG(grade) AS average_grade FROM Grades GROUP BY course_id; --
Average grade for a course
SELECT course_id, MAX(attendance_percentage), MIN(attendance_percentage)
FROM Attendance GROUP BY course_id; -- Max and min attendance percentage for a
course
---
-- 5. Joins
SELECT s.student_id, s.student_name, d.department_name
FROM Students s
JOIN Departments d ON s.department_id = d.department_id; -- Retrieve all students
and their respective departments
SELECT e.enrollment_id, s.student_name, c.course_name
FROM Enrollments e
JOIN Students s ON e.student_id = s.student_id
JOIN Courses c ON e.course_id = c.course_id; -- Retrieve course enrollments with
student names
SELECT t.teacher_id, t.teacher_name, c.course_name
FROM Teachers t
JOIN Courses c ON t.teacher_id = c.teacher_id; -- Retrieve teacher information
along with the courses they teach
---
-- 6. Subqueries
SELECT * FROM Students WHERE student_id IN (
SELECT student_id FROM Enrollments WHERE course_id = 202
); -- Students enrolled in a specific course
SELECT course_id FROM Enrollments
GROUP BY course_id
HAVING COUNT(student_id) > 5; -- Courses with more than 5 enrollments
SELECT department_name FROM Departments
WHERE department_id NOT IN (
SELECT DISTINCT department_id FROM Students
); -- Departments with no students
---
-- 7. Advanced Queries
SELECT course_id, COUNT(student_id) AS total_enrolled
FROM Enrollments
GROUP BY course_id
ORDER BY total_enrolled DESC
LIMIT 1; -- Retrieve courses with the highest enrollment
SELECT student_id, course_id FROM Attendance WHERE attendance_percentage = 100; --
Retrieve students with perfect attendance
SELECT teacher_id, COUNT(course_id) AS total_courses
FROM Courses
GROUP BY teacher_id
ORDER BY total_courses DESC
LIMIT 1; -- Retrieve teachers teaching the most courses
---
-- 8. Data Modification
UPDATE Attendance
SET attendance_percentage = 95
WHERE student_id = 1001 AND course_id = 202; -- Update attendance for a specific
student
DELETE FROM Students WHERE department_id = 105; -- Delete students from a specific
department
INSERT INTO Courses (course_id, course_name, credits, semester, teacher_id)
VALUES (303, 'Data Science', 4, 6, 12); -- Insert a new course
---
-- 9. Database Administration
ALTER TABLE Students ADD COLUMN email VARCHAR(255); -- Add a new column to the
Students table
CREATE INDEX idx_department_id ON Students(department_id); -- Create an index on
the Students table
GRANT SELECT, INSERT ON StudentManagement.* TO 'user'@'localhost'; -- Grant
permissions to a user
---
-- 10. Views
CREATE VIEW StudentEnrollments AS
SELECT s.student_name, c.course_name
FROM Enrollments e
JOIN Students s ON e.student_id = s.student_id
JOIN Courses c ON e.course_id = c.course_id; -- Create a view for student
enrollments
SELECT * FROM StudentEnrollments; -- Retrieve data from the view