forked from skillrepos/copilot-dd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.sql
63 lines (59 loc) · 1.6 KB
/
dev.sql
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
-- define a select statement to get all students enrolled in a course
-- and the course name, location name, and instructor name.
SELECT
students.first_name,
students.last_name,
subjects.course_name,
locations.location_name,
staffs.first_name AS instructor_first_name,
staffs.last_name AS instructor_last_name
FROM
courses.students
JOIN
courses.registrations
ON
students.student_id = registrations.student_id
JOIN
courses.registration_items
ON
registrations.registration_id = registration_items.registration_id
JOIN
curriculum.subjects
ON
registration_items.course_id = subjects.course_id
JOIN
courses.staffs
ON
registrations.staff_id = staffs.staff_id
JOIN
courses.locations
ON
registrations.location_id = locations.location_id;
-- write an index to improve the performance of the query
CREATE INDEX idx_student_id ON courses.students (student_id);
CREATE INDEX idx_registration_id ON courses.registrations (registration_id);
CREATE INDEX idx_course_id ON curriculum.subjects (course_id);
-- define a stored procedure to get course enrollment by location
CREATE PROCEDURE courses.get_course_enrollment_by_location
@location_id INT
AS
BEGIN
SELECT
subjects.course_name,
COUNT(registrations.registration_id) AS enrollment
FROM
courses.registrations
JOIN
courses.registration_items
ON
registrations.registration_id = registration_items.registration_id
JOIN
curriculum.subjects
ON
registration_items.course_id = subjects.course_id
WHERE
registrations.location_id = @location_id
GROUP BY
subjects.course_name;
END;
```