-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab 3.sql
More file actions
206 lines (177 loc) · 6.12 KB
/
Lab 3.sql
File metadata and controls
206 lines (177 loc) · 6.12 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
USE CSE_4B_369
--------- Lab - 3 Advanced Stored Procedure
--------------------------------------------Part – A ------------------------------------------------
-- 1. Create a stored procedure that accepts a date and returns all faculty members who joined on that date.
CREATE OR ALTER PROCEDURE SP_FACULTY_JOINING_DATE
@JoiningDate DATE
AS
BEGIN
SELECT FacultyName FROM FACULTY
WHERE FacultyJoiningDate = @JoiningDate
END
GO
-- 2. Create a stored procedure for ENROLLMENT table where user enters either StudentID and returns EnrollmentID, EnrollmentDate, Grade, and Status.
CREATE OR ALTER PROCEDURE SP_ENROLL_STATS_OR_CID
@StuId INT = NULL,
@CID VARCHAR(10) = NULL
AS
BEGIN
SELECT EnrollmentID , EnrollmentDate , Grade , EnrollmentStatus
FROM ENROLLMENT
WHERE StudentID = @StuId
OR CourseID = @CID
END
EXEC SP_ENROLL_STATS_OR_CID '1'
EXEC SP_ENROLL_STATS_OR_CID @CID = 'CS101'
GO
-- 3. Create a stored procedure that accepts two integers (min and max credits) and returns all courses whose credits fall between these values.
CREATE OR ALTER PROCEDURE SP_COURSE_CREDIT_STATS
@minCredit INT,
@maxCredit INT
AS
BEGIN
SELECT CourseName
FROM COURSE
-- WHERE CourseCredits BETWEEN @minCredit AND @maxCredit;
WHERE CourseCredits > @minCredit AND CourseCredits < @maxCredit
END
GO
-- 4. Create a stored procedure that accepts Course Name and returns the list of students enrolled in that course.
CREATE OR ALTER PROCEDURE SP_COURSE_WITH_STUDENTS
@courseName varchar(50)
AS
BEGIN
SELECT C.CourseName , S.StuName
FROM COURSE C
JOIN ENROLLMENT E
ON C.CourseID = E.CourseID
JOIN STUDENT S
ON E.StudentID = S.StudentID
WHERE C.CourseName = @courseName
END
GO
-- 5. Create a stored procedure that accepts Faculty Name and returns all course assignments.
CREATE OR ALTER PROCEDURE SP_FACULTY_WITH_COURSE
@facultyName VARCHAR(50)
AS
BEGIN
SELECT F.FacultyName, C.CourseName, CA.Semester, CA.Year, CA.ClassRoom
FROM FACULTY F
JOIN COURSE_ASSIGNMENT CA
ON F.FacultyID = CA.FacultyID
JOIN COURSE C
ON CA.CourseID = C.CourseID
WHERE F.FacultyName = @facultyName
END
GO
-- 6. Create a stored procedure that accepts Semester number and Year, and returns all course assignments with faculty and classroom details.
CREATE OR ALTER PROCEDURE SP_COURSE_ASSIGNMENTS_BY_SEMESTER
@semester INT,
@year INT
AS
BEGIN
SELECT C.CourseName, F.FacultyName, CA.Semester, CA.Year, CA.ClassRoom
FROM COURSE_ASSIGNMENT CA
JOIN COURSE C
ON CA.CourseID = C.CourseID
JOIN FACULTY F
ON CA.FacultyID = F.FacultyID
WHERE CA.Semester = @semester AND CA.Year = @year
END
GO
---------------------------------------- Part B----------------------------------------------
-- 7. Create a stored procedure that accepts the first letter of Status ('A', 'C', 'D') and returns enrollment details.
CREATE OR ALTER PROCEDURE SP_ENROLLMENT_BY_STATUS_LETTER
@statusLetter CHAR(1)
AS
BEGIN
SELECT E.EnrollmentID, E.StudentID, S.StuName, E.CourseID, C.CourseName,
E.EnrollmentDate, E.Grade, E.EnrollmentStatus
FROM ENROLLMENT E
JOIN STUDENT S ON E.StudentID = S.StudentID
JOIN COURSE C ON E.CourseID = C.CourseID
WHERE LEFT(E.EnrollmentStatus, 1) = @statusLetter
END
GO
-- 8. Create a stored procedure that accepts either Student Name OR Department Name and returns student data accordingly.
CREATE OR ALTER PROCEDURE SP_STUDENT_BY_NAME_OR_DEPT
@Name VARCHAR(100)
AS
BEGIN
SELECT * FROM STUDENT
WHERE StuName = @Name OR StuDepartment = @Name
END
EXEC SP_STUDENT_BY_NAME_OR_DEPT 'Raj Patel'
GO
-- 9. Create a stored procedure that accepts CourseID and returns all students enrolled grouped by enrollment status with counts.
CREATE OR ALTER PROCEDURE SP_STUDENTS_BY_COURSE_STATUS
@courseID VARCHAR(10)
AS
BEGIN
SELECT E.EnrollmentStatus, COUNT(*) as StudentCount ,S.StuName
FROM ENROLLMENT E
JOIN STUDENT S ON E.StudentID = S.StudentID
WHERE E.CourseID = @courseID
GROUP BY E.EnrollmentStatus
END
GO
---------------------------------------------- Part C------------------------------------------------
-- 10. Create a stored procedure that accepts a year as input and returns all courses assigned to faculty in that year with classroom details.
CREATE OR ALTER PROCEDURE SP_COURSES_BY_YEAR
@year INT
AS
BEGIN
SELECT C.CourseID, C.CourseName, F.FacultyName, CA.Semester, CA.Year, CA.ClassRoom
FROM COURSE_ASSIGNMENT CA
JOIN COURSE C ON CA.CourseID = C.CourseID
JOIN FACULTY F ON CA.FacultyID = F.FacultyID
WHERE CA.Year = @year
ORDER BY CA.Semester, C.CourseName
END
GO
-- 11. Create a stored procedure that accepts From Date and To Date and returns all enrollments within that range with student and course details.
CREATE OR ALTER PROCEDURE SP_ENROLLMENTS_BY_DATE_RANGE
@fromDate DATE,
@toDate DATE
AS
BEGIN
SELECT E.EnrollmentID, E.EnrollmentDate, S.StuName, S.StuDepartment,
C.CourseName, C.CourseCredits, E.Grade, E.EnrollmentStatus
FROM ENROLLMENT E
JOIN STUDENT S ON E.StudentID = S.StudentID
JOIN COURSE C ON E.CourseID = C.CourseID
WHERE E.EnrollmentDate BETWEEN @fromDate AND @toDate
ORDER BY E.EnrollmentDate
END
GO
-- 12. Create a stored procedure that accepts FacultyID and calculates their total teaching load (sum of credits of all courses assigned).
CREATE OR ALTER PROCEDURE SP_FACULTY_TEACHING_LOAD
@facultyID INT
AS
BEGIN
SELECT F.FacultyID, F.FacultyName, F.FacultyDepartment,
SUM(C.CourseCredits) as TotalTeachingLoad,
COUNT(CA.CourseID) as TotalCoursesAssigned
FROM FACULTY F
JOIN COURSE_ASSIGNMENT CA ON F.FacultyID = CA.FacultyID
JOIN COURSE C ON CA.CourseID = C.CourseID
WHERE F.FacultyID = @facultyID
GROUP BY F.FacultyID, F.FacultyName, F.FacultyDepartment
END
GO
-- OUTPUT PARAMETER
-- find the number of courses offered by given department
CREATE OR ALTER PROC SP_COURSE_BY_DEPARTMENT
@department VARCHAR(100),
@count INT OUT
AS
BEGIN
SELECT @count = COUNT(*)
FROM COURSE
WHERE CourseDepartment = @department
END
GO
DECLARE @COUNT INT
EXEC SP_COURSE_BY_DEPARTMENT @department = 'CSE' , @COUNT = @count OUTPUT
SELECT @COUNT AS Course_count
-- SELECT * FROM COURSE