-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab 1.sql
More file actions
178 lines (118 loc) · 5.31 KB
/
Lab 1.sql
File metadata and controls
178 lines (118 loc) · 5.31 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
----------------------------------------LAB 1--------------------------------------------
--------------------------------------Part – A -------------------------------------------
--1. Retrieve all unique departments from the STUDENT table.
SELECT DISTINCT StuDepartment
FROM STUDENT
--2. Insert a new student record into the STUDENT table.
--(9, 'Neha Singh', 'neha.singh@univ.edu', '9876543218', 'IT', '2003-09-20', 2021)
INSERT INTO STUDENT
VALUES(9, 'Neha Singh', 'neha.singh@univ.edu', '9876543218', 'IT', '2003-09-20', 2021)
--3. Change the Email of student 'Raj Patel' to 'raj.p@univ.edu'. (STUDENT table)
UPDATE STUDENT
SET STUEMAIL = 'raj.p@univ.edu'
WHERE STUNAME = 'RAJ PATEL'
--4. Add a new column 'CGPA' with datatype DECIMAL(3,2) to the STUDENT table.
ALTER TABLE STUDENT
ADD CGPA DECIMAL(3,2)
--5. Retrieve all courses whose CourseName starts with 'Data'. (COURSE table)
SELECT * FROM COURSE
WHERE CourseName LIKE 'DATA%'
--6. Retrieve all students whose Name contains 'Shah'. (STUDENT table)
SELECT * FROM STUDENT
WHERE STUName LIKE '%SHAH%'
--7. Display all Faculty Names in UPPERCASE. (FACULTY table)
SELECT UPPER (FacultyName)
FROM FACULTY
--8. Find all faculty who joined after 2015. (FACULTY table)
SELECT *
FROM FACULTY
WHERE FacultyJoiningDate > 2015
--9. Find the SQUARE ROOT of Credits for the course 'Database Management Systems'. (COURSE table)
SELECT SQRT(CourseCredits)
FROM COURSE
WHERE CourseName = 'Database Management Systems'
--10. Find the Current Date using SQL Server in-built function.
SELECT GETDATE()
--11. Find the top 3 students who enrolled earliest (by EnrollmentYear). (STUDENT table)
SELECT TOP 3 * FROM STUDENT
ORDER BY StuEnrollmentYear ASC;
--12. Find all enrollments that were made in the year 2022. (ENROLLMENT table)
SELECT * FROM ENROLLMENT
WHERE YEAR(EnrollmentDate) = 2022;
--13. Find the number of courses offered by each department. (COURSE table)
SELECT CourseDepartment, COUNT(*) AS CourseCount
FROM COURSE
GROUP BY CourseDepartment;
--14. Retrieve the CourseID which has more than 2 enrollments. (ENROLLMENT table)
SELECT CourseID FROM ENROLLMENT
GROUP BY CourseID
HAVING COUNT(*) > 2;
--15. Retrieve all the student name with their enrollment status. (STUDENT & ENROLLMENT table)
SELECT S.StuName, E.EnrollmentStatus
FROM STUDENT S
JOIN ENROLLMENT E
ON S.StudentID = E.StudentID;
--16. Select all student names with their enrolled course names. (STUDENT, COURSE, ENROLLMENT table)
SELECT S.StuName, C.CourseName
FROM STUDENT S
JOIN ENROLLMENT E ON S.StudentID = E.StudentID
JOIN COURSE C ON E.CourseID = C.CourseID;
--17. Create a view called 'ActiveEnrollments' showing only active enrollments with student name and course name. (STUDENT, COURSE, ENROLLMENT, table)
CREATE VIEW ActiveEnrollments AS
SELECT S.StuName, C.CourseName, E.EnrollmentStatus
FROM STUDENT S
JOIN ENROLLMENT E ON S.StudentID = E.StudentID
JOIN COURSE C ON E.CourseID = C.CourseID
WHERE E.EnrollmentStatus = 'Active';
--18. Retrieve the student’s name who is not enrol in any course using subquery. (STUDENT, ENROLLMENT TABLE)
SELECT StuName
FROM STUDENT
WHERE StudentID NOT IN (SELECT StudentID FROM ENROLLMENT);
--19. Display course name having second highest credit. (COURSE table)
SELECT TOP 1 CourseName
FROM COURSE
WHERE CourseCredits < (SELECT MAX(CourseCredits)
FROM COURSE)
ORDER BY CourseCredits DESC;
---------------------------------------Part – B--------------------------------------------
--20. Retrieve all courses along with the total number of students enrolled. (COURSE, ENROLLMENT table)
SELECT C.CourseID, C.CourseName, COUNT(E.StudentID) AS EnrolledStudents
FROM COURSE C
LEFT JOIN ENROLLMENT E ON C.CourseID = E.CourseID
GROUP BY C.CourseID, C.CourseName;
--21. Retrieve the total number of enrollments for each status, showing only statuses that have more than 2 enrollments. (ENROLLMENT table)
SELECT EnrollmentStatus, COUNT(*) AS TotalEnrollments
FROM ENROLLMENT
GROUP BY EnrollmentStatus
HAVING COUNT(*) > 2;
--22. Retrieve all courses taught by 'Dr. Sheth' and order them by Credits. (FACULTY, COURSE, COURSE_ASSIGNMENT table)
SELECT C.CourseID, C.CourseName, C.CourseCredits
FROM FACULTY F
JOIN COURSE_ASSIGNMENT CA ON F.FacultyID = CA.FacultyID
JOIN COURSE C ON CA.CourseID = C.CourseID
WHERE F.FacultyName = 'Dr. Sheth'
ORDER BY C.CourseCredits;
---------------------------------------Part – C----------------------------------------------
--23. List all students who are enrolled in more than 3 courses. (STUDENT, ENROLLMENT table)
SELECT S.StudentID, S.StuName, COUNT(E.EnrollmentID) AS CourseCount
FROM STUDENT S
JOIN ENROLLMENT E ON S.StudentID = E.StudentID
GROUP BY S.StudentID, S.StuName
HAVING COUNT(E.EnrollmentID) > 3;
--24. Find students who have enrolled in both 'CS101' and 'CS201' Using Sub Query. (STUDENT, ENROLLMENT table)
SELECT StuName
FROM STUDENT
WHERE StudentID IN (
SELECT StudentID
FROM ENROLLMENT
WHERE CourseID = 'CS101'
INTERSECT
SELECT StudentID
FROM ENROLLMENT
WHERE CourseID = 'CS201'
);
--25. Retrieve department-wise count of faculty members along with their average years of experience (calculate experience from JoiningDate). (Faculty table)
SELECT FacultyDepartment, COUNT(*) AS FACULTYCOUNT,
AVG(DATEDIFF(YEAR, FacultyJoiningDate, GETDATE())) AS AVGYEAROFEXPERIENCE
FROM FACULTY
GROUP BY FacultyDepartment