-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab 2.sql
More file actions
177 lines (136 loc) · 4.37 KB
/
Lab 2.sql
File metadata and controls
177 lines (136 loc) · 4.37 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
------Lab-2 Stored Procedure
-- Part – A
--1.INSERT Procedures: Create stored procedures to insert records into STUDENT tables (SP_INSERT_STUDENT)
--StuID Name Email Phone Department DOB EnrollmentYear
--10 Harsh Parmar harsh@univ.edu 9876543218 CSE 2005-09-18 2023
--20 Om Patel om@univ.edu 9876543211 IT 2002-08-22 2022
CREATE OR ALTER PROC PR_INSERT_STUDENT
@StudentID INT ,
@StuName VARCHAR(100) ,
@StuEmail VARCHAR(100) ,
@StuPhone VARCHAR(15) ,
@StuDept VARCHAR(50),
@DOB DATE ,
@StuEnrollmentYear INT
AS
BEGIN
INSERT INTO STUDENT (StudentID,StuName,StuEmail,StuPhone,StuDepartment,StuDateOfBirth,StuEnrollmentYear)
VALUES(@StudentID,@StuName,@StuEmail,@StuPhone,@StuDept,@DOB,@StuEnrollmentYear)
END
EXEC PR_INSERT_STUDENT 10 ,'Harsh Parmar', 'harsh@univ.edu', '9876543218', 'CSE','2005-09-18',2023;
EXEC PR_INSERT_STUDENT 20 ,'Om Patel', 'om@univ.edu', '9876543211', 'IT','2002-08-22',2022;
SELECT * FROM STUDENT
--2. INSERT Procedures: Create stored procedures to insert records into COURSE tables
--(SP_INSERT_COURSE)
CREATE OR ALTER PROC PR_INSERT_COURSE
@CourseID VARCHAR(10),
@CourseName VARCHAR(100),
@CourseCredits INT ,
@CourseDepartment VARCHAR(50),
@CourseSemester INT
AS
BEGIN
INSERT INTO COURSE(CourseID,CourseName,CourseCredits,CourseDepartment,CourseSemester)
VALUES(@CourseID,@CourseName,@CourseCredits ,@CourseDepartment,@CourseSemester)
END
EXEC PR_INSERT_COURSE 'CS330','Computer Networks',4, 'CSE', 5
EXEC PR_INSERT_COURSE 'EC120','Electronic Circuits',3, 'ECE', 2
SELECT * FROM COURSE
--3. UPDATE Procedures: Create stored procedure SP_UPDATE_STUDENT to update Email and Phone in STUDENT table. (Update using studentID)
CREATE OR ALTER PROC PR_UPDATE_STUDENT
@StudentID INT ,
@StuEmail VARCHAR(100) ,
@StuPhone VARCHAR(15)
AS
BEGIN
UPDATE STUDENT
SET StuEmail = @StuEmail,
StuPhone = @StuPhone
WHERE StudentID = @StudentID
END
EXEC PR_UPDATE_STUDENT
--4. DELETE Procedures: Create stored procedure SP_DELETE_STUDENT to delete records from STUDENT where Student Name is Om Patel.
CREATE OR ALTER PROC PR_DELETE_STUDENT
@StuName VARCHAR(100)
AS
BEGIN
DELETE
FROM STUDENT
WHERE StuName = @StuName
END
EXEC PR_DELETE_STUDENT 'Om Patel';
SELECT * FROM STUDENT
--5.SELECT BY PRIMARY KEY: Create stored procedures to select records by primary key (SP_SELECT_STUDENT_BY_ID) from Student table.
CREATE OR ALTER PROC PR_SELECT_STUDENT_BY_ID
@StudentID INT
AS
BEGIN
SELECT *
FROM STUDENT
WHERE StudentID = @StudentID
END
EXEC PR_SELECT_STUDENT_BY_ID ;
SELECT * FROM STUDENT
--6. Create a stored procedure that shows details of the first 5 students ordered by EnrollmentYear.
CREATE OR ALTER PROC PR_TOP5_STUDENTS_BY_ENROLLMENT
AS
BEGIN
SELECT TOP 5 *
FROM STUDENT
ORDER BY StuEnrollmentYear
END
EXEC PR_TOP5_STUDENTS_BY_ENROLLMENT
-- Part – B
--7. Create a stored procedure which displays faculty designation-wise count.
CREATE OR ALTER PROC PR_FACULTY_DESIGNATION_COUNT
@FacultyID INT ,
@FacultyName VARCHAR(100),
@FacultyDepartment VARCHAR(50) ,
@FacultyDesignation VARCHAR(50)
AS
BEGIN
SELECT FacultyDesignation, COUNT(*) AS FACULTY_COUNT
FROM FACULTY
GROUP BY FacultyDesignation
END
EXEC PR_FACULTY_DESIGNATION_COUNT
--8. Create a stored procedure that takes department name as input and returns all students in that department.
CREATE OR ALTER PROC PR_GET_STUDENTS_BY_DEPARTMENT
@DepartmentName VARCHAR(50)
AS
BEGIN
SELECT *
FROM STUDENT
WHERE StuDepartment = @DepartmentName
ORDER BY StuName
END
EXEC PR_GET_STUDENTS_BY_DEPARTMENT 'CSE'
-- Part – C
--9. Create a stored procedure which displays department-wise maximum, minimum, and average credits of courses.
CREATE OR ALTER PROC PR_DEPT_WISE_CREDITS_COURSE
AS
BEGIN
SELECT
MAX(CourseCredits) AS MAXCREDITS,
MIN(CourseCredits) AS MINCREDITS,
AVG(CourseCredits) AS AVGCREDITS,
CourseDepartment
FROM COURSE
GROUP BY CourseDepartment
END
EXEC PR_DEPT_WISE_CREDITS_COURSE
--10.Create a stored procedure that accepts StudentID as parameter and returns all courses the student is enrolled in with their grades.
CREATE OR ALTER PROC PR_GET_STUDENT_COURSES_GRADES
@StudentID INT
AS
BEGIN
SELECT CourseName, Grade
FROM STUDENT S
INNER JOIN ENROLLMENT E
ON S.StudentID = E.StudentID
INNER JOIN COURSE C
ON E.CourseID = C.CourseID
WHERE S.StudentID = @StudentID
ORDER BY C.CourseName
END
EXEC PR_GET_STUDENT_COURSES_GRADES 5