-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFINAL PROJECT__University Course Management System.txt
More file actions
210 lines (155 loc) · 5.24 KB
/
Copy pathFINAL PROJECT__University Course Management System.txt
File metadata and controls
210 lines (155 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
==============================
1️⃣ Students Table
==============================
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
BirthDate DATE,
EnrollmentDate DATE
);
INSERT INTO Students (StudentID,FirstName,LastName,Email,BirthDate,EnrollmentDate) VALUES
(1,'John','Doe','john.doe@email.com','2000-01-15','2022-08-01'),
(2, 'Jane', 'Smith', 'jane.smith@email.com', '1999-05-25', '2021-08-01');
==============================
2️⃣ Courses Table
==============================
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100),
DepartmentID INT,
Credits INT
);
INSERT INTO Courses (CourseID,CourseName,Department,Credits) VALUES
(101, 'Introduction to SQL', 1, 3),
(102, 'Data Structures', 2, 4);
==============================
3️⃣ Instructors Table
==============================
CREATE TABLE Instructors (
InstructorID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
DepartmentID INT,
Salary DECIMAL(10,2)
);
INSERT INTO Instructors (InstructorID, FirstName,LastName,Email,DepartmentID,Salary) VALUES
(1, 'Alice', 'Johnson', 'alice.johnson@univ.com', 1, 80000),
(2, 'Bob', 'Lee', 'bob.lee@univ.com', 2, 75000);
==============================
4️⃣ Enrollments Table
==============================
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE
);
INSERT INTO Enrollments(EnrollmentID,StudentID,CourseID,EnrollmentDate) VALUES
(1, 1, 101, '2022-08-01'),
(2, 2, 102, '2021-08-01');
==============================
5️⃣ Departments Table
==============================
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(100)
);
INSERT INTO Departments (DepartmentID,DepartmentName) VALUES
(1, 'Computer Science'),
(2, 'Mathematics');
==============================
Queries
==============================
1️⃣ Perform CRUD Operations :-
INSERT INTO Students VALUES
(3, 'Ali', 'Khan', 'ali@email.com', '2001-03-10', '2023-08-01');
SELECT * FROM Students;
UPDATE Students
SET Email = 'ali.khan@email.com'
WHERE StudentID = 3;
DELETE FROM Students
WHERE StudentID = 3;
2️⃣ Retrieve students who enrolled after 2022 :-
SELECT *
FROM Students
WHERE EnrollmentDate > '2022-12-31';
3️⃣ Courses offered by Mathematics department (limit 5) :-
SELECT *
FROM Courses
WHERE DepartmentID = 2
LIMIT 5;
4️⃣ Number of students in each course (more than 5) :-
SELECT CourseID, COUNT(StudentID) AS TotalStudents
FROM Enrollments
GROUP BY CourseID
HAVING COUNT(StudentID) > 5;
5️⃣ Students enrolled in both Introduction to SQL and Data Structures :-
SELECT s.StudentID, s.FirstName
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
WHERE e.CourseID IN (101,102)
GROUP BY s.StudentID, s.FirstName
HAVING COUNT(DISTINCT e.CourseID) = 2;
6️⃣ Students enrolled in either Introduction to SQL OR Data Structures :-
SELECT DISTINCT s.StudentID, s.FirstName
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
WHERE e.CourseID IN (101,102);
7️⃣ Average credits of all courses :-
SELECT AVG(Credits) AS AverageCredits
FROM Courses;
8️⃣ Maximum salary of instructors in Computer Science department :-
SELECT MAX(Salary) AS MaxSalary
FROM Instructors
WHERE DepartmentID = 1;
9️⃣ Count students enrolled in each department :-
SELECT d.DepartmentName, COUNT(e.StudentID) AS TotalStudents
FROM Departments d
JOIN Courses c ON d.DepartmentID = c.DepartmentID
JOIN Enrollments e ON c.CourseID = e.CourseID
GROUP BY d.DepartmentName;
🔟 INNER JOIN – Students and their courses :-
SELECT s.FirstName, c.CourseName
FROM Students s
INNER JOIN Enrollments e ON s.StudentID = e.StudentID
INNER JOIN Courses c ON e.CourseID = c.CourseID;
1️⃣1️⃣ LEFT JOIN – All students and courses (if any) :-
SELECT s.FirstName, c.CourseName
FROM Students s
LEFT JOIN Enrollments e ON s.StudentID = e.StudentID
LEFT JOIN Courses c ON e.CourseID = c.CourseID;
1️⃣2️⃣ Subquery – Students in courses with more than 10 students :-
SELECT *
FROM Students
WHERE StudentID IN (
SELECT StudentID
FROM Enrollments
WHERE CourseID IN (
SELECT CourseID
FROM Enrollments
GROUP BY CourseID
HAVING COUNT(StudentID) > 10
)
);
1️⃣3️⃣ Extract Year from EnrollmentDate :-
SELECT StudentID, YEAR(EnrollmentDate) AS EnrollmentYear
FROM Students;
1️⃣4️⃣ Concatenate instructor first and last name :-
SELECT CONCAT(FirstName, ' ', LastName) AS FullName
FROM Instructors;
1️⃣5️⃣ Running total of students in courses :-
SELECT CourseID,
COUNT(StudentID) AS TotalStudents,
SUM(COUNT(StudentID)) OVER (ORDER BY CourseID) AS RunningTotal
FROM Enrollments
GROUP BY CourseID;
1️⃣6️⃣ Label students as Senior or Junior :-
SELECT StudentID,
CASE
WHEN TIMESTAMPDIFF(YEAR, EnrollmentDate, CURDATE()) > 4 THEN 'Senior'
ELSE 'Junior'
END AS Status
FROM Students;