-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.sql
119 lines (101 loc) · 3.6 KB
/
query.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
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
-- A list of all the students
SELECT *
FROM Student
-- A list of all the trainers
SELECT *
FROM Trainer
-- A list of all the assignments
SELECT *
FROM Assignment
-- A list of all the courses
SELECT *
FROM Course
-- All the Students per Course
SELECT S.ID AS StudentID, S.FName AS First_Name, S.LName AS LastName,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type
FROM Student S, Course C, StudentPerCourse SPC
WHERE S.ID = SPC.StudentID AND C.ID = SPC.CourseID
ORDER BY C.Title, S.FName
-- All the Students per Course (using JOIN)
SELECT S.ID AS StudentID, S.FName AS First_Name, S.LName AS LastName,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type
FROM Student S
JOIN StudentPerCourse SPC
ON S.ID = SPC.StudentID
JOIN Course C
ON C.ID = SPC.CourseID
ORDER BY C.Title, S.FName
-- All the Trainers per Course
SELECT T.ID AS TrainerID, T.FName AS First_Name, T.LName AS LastName,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type
FROM Trainer T, Course C
WHERE T.ID = C.TrainerID
ORDER BY T.ID
-- All the Trainers per Course (using JOIN)
SELECT T.ID AS TrainerID, T.FName AS First_Name, T.LName AS LastName,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type
FROM Trainer T
JOIN Course C
ON T.ID = C.TrainerID
ORDER BY T.ID
-- All the Assignments per Course
SELECT A.ID AS AssignmentID, A.Title AS Title, A.Description AS Description,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type
FROM Assignment A, Course C, AssignmentPerCourse APC
WHERE A.ID = APC.AssignmentID AND C.ID = APC.CourseID
ORDER BY C.ID
-- All the Assignments per Course (using JOIN)
SELECT A.ID AS AssignmentID, A.Title AS Title, A.Description AS Description,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type
FROM Assignment A
JOIN AssignmentPerCourse APC
ON A.ID = APC.AssignmentID
JOIN Course C
ON C.ID = APC.CourseID
ORDER BY C.ID
-- All the Assignments per Course per Student
SELECT A.ID AS AssignmentID, A.Title AS Title, A.Description AS Description,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type, S.ID AS StudentID, S.FName AS FirstName, S.LName AS LastName
FROM Assignment A, Course C, Student S, AssignmentPerCourse APC, StudentPerCourse SPC
WHERE A.ID = APC.AssignmentID AND C.ID = APC.CourseID
AND S.ID = SPC.StudentID AND C.ID = SPC.CourseID
ORDER BY A.ID
-- All the Assignments per Course per Student (using JOIN)
SELECT A.ID AS AssignmentID, A.Title AS Title, A.Description AS Description,
C.ID AS CourseID, C.Title AS Course_Title, C.Stream AS Course_Stream,
C.Type AS Course_Type, S.ID AS StudentID, S.FName AS FirstName, S.LName AS LastName
FROM Assignment A
JOIN AssignmentPerCourse APC
ON A.ID = APC.AssignmentID
JOIN Course C
ON C.ID = APC.CourseID
JOIN StudentPerCourse SPC
ON SPC.CourseID = C.ID
JOIN Student S
ON S.ID = SPC.StudentID
ORDER BY A.ID
-- A list of Students that belong to more than one Courses
SELECT S.ID AS StudentID, S.FName AS FirstName, S.LName AS LastName,
COUNT(*) AS NumberOfCourses
FROM Student S, StudentPerCourse SPC, Course C
WHERE S.ID = SPC.StudentID
AND C.ID = SPC.CourseID
GROUP BY S.ID, S.FName, S.LName
HAVING COUNT(*) > 1
-- A list of Students that belong to more than one Courses (using JOIN)
SELECT S.ID AS StudentID, S.FName AS FirstName, S.LName AS LastName,
COUNT(*) AS NumberOfCourses
FROM Student S
JOIN StudentPerCourse SPC
ON S.ID = SPC.StudentID
JOIN Course C
ON C.ID = SPC.CourseID
GROUP BY S.ID, S.FName, S.LName
HAVING COUNT(*) > 1