-
Notifications
You must be signed in to change notification settings - Fork 0
/
Students_Subject_Relashionship_Data.sql
187 lines (154 loc) · 4.35 KB
/
Students_Subject_Relashionship_Data.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
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
CREATE DATABASE Institute_Data;
use Institute_data;
-- create a table named - Student
CREATE TABLE Student (
student_id VARCHAR(10) NOT NULL,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
Class INT NOT NULL,
PRIMARY KEY (student_id)
);
-- create a table named - Subject
CREATE TABLE Subject (
Subject_id VARCHAR(10) NOT NULL,
Subject_name VARCHAR(15),
PRIMARY KEY (Subject_id)
);
-- create a table named - Student_subject_relationship
CREATE TABLE Student_subject_relationship (
Student_id VARCHAR(10) NOT NULL,
Subject_id VARCHAR(15) NOT NULL,
FOREIGN KEY (Student_id)
REFERENCES Student (student_id),
FOREIGN KEY (Subject_id)
REFERENCES Subject (Subject_id)
);
-- add values into Student tables
INSERT INTO Student VALUES
('Stud_1','Farookh Siddiui',22, 12),
('Stud_2','Ayaz Chaudhary',21,11),
('Stud_3','Mridul Sharma',22,12),
('Stud_4','Ali Siddiqui',13,7),
('Stud_5','Vaibhav Wakde',20,10),
('Stud_6','Vansh Rana',22,11),
('Stud_7','Anshu Gupta',22,11),
('Stud_8','Shorya Chaudhary',23,12),
('Stud_9','Aryan Sarang',18,10),
('Stud_10','Aadil Hussain',20,11),
('Stud_11','Tarun Shyam',22,12);
-- add values into Subject table
INSERT INTO Subject VALUES
('Sub_1','English'),
('Sub_2','Hindi'),
('Sub_3','Maths'),
('Sub_4','Science');
-- add values into Student_subject_relationship table
INSERT INTO Student_subject_relationship VALUES
('Stud_1','Sub_1'),
('Stud_1','Sub_2'),
('Stud_1','Sub_4'),
('Stud_2','Sub_2'),
('Stud_2','Sub_3'),
('Stud_2','Sub_4'),
('Stud_3','Sub_1'),
('Stud_3','Sub_2'),
('Stud_3','Sub_3'),
('Stud_3','Sub_4'),
('Stud_4','Sub_3'),
('Stud_5','Sub_3'),
('Stud_5','Sub_4'),
('Stud_6','Sub_4'),
('Stud_7','Sub_1'),
('Stud_7','Sub_2'),
('Stud_8','Sub_1'),
('Stud_8','Sub_3'),
('Stud_8','Sub_4'),
('Stud_9','Sub_1'),
('Stud_9','Sub_2'),
('Stud_9','Sub_3'),
('Stud_9','Sub_4'),
('Stud_10','Sub_2'),
('Stud_10','Sub_3'),
('Stud_10','Sub_4'),
('Stud_11','Sub_3');
-- Query to get all 3 tables combined into 1 table
SELECT
*
FROM
Student AS stud
JOIN
student_subject_relationship AS Stud_sub ON stud.student_id = Stud_sub.Student_id
JOIN
Subject AS Sub ON Stud_sub.Subject_id = Sub.Subject_id;
-- Query to get the list of students enrolled only in "Science"
SELECT
s.student_id, s.name
FROM
student s
LEFT JOIN
student_subject_relationship r ON s.student_id = r.student_id
LEFT JOIN
Subject su ON r.Subject_id = su.Subject_id
WHERE
su.Subject_name = 'Science'
AND r.student_id NOT IN (SELECT
ssr.student_id
FROM
student_subject_relationship ssr
WHERE
ssr.subject_id <> (SELECT
subject_id
FROM
subject
WHERE
subject_name = 'Science'));
-- Query to get the list of students enrolled only in "Maths"
SELECT
s.student_id, s.name
FROM
student s
LEFT JOIN
student_subject_relationship r ON s.student_id = r.student_id
LEFT JOIN
Subject su ON r.Subject_id = su.Subject_id
WHERE
su.Subject_name = 'Maths'
AND r.student_id NOT IN (SELECT
ssr.student_id
FROM
student_subject_relationship ssr
WHERE
ssr.subject_id != (SELECT
subject_id
FROM
subject
WHERE
subject_name = 'Maths'));
-- Query to get the list of students enrolled only in "Science" and "Maths"
SELECT
s.student_id, s.name
FROM
student s
INNER JOIN
student_subject_relationship r ON s.student_id = r.student_id
INNER JOIN
subject su ON r.subject_id = su.subject_id
WHERE
su.subject_name IN ('Math' , 'Science')
GROUP BY s.student_id , s.name
HAVING COUNT(DISTINCT su.subject_name) = 2;
-- Query to get the list of students enrolled in all 4 subjectss
SELECT
s.student_id, s.name
FROM
student s
INNER JOIN
student_subject_relationship r ON s.student_id = r.student_id
INNER JOIN
subject su ON r.subject_id = su.subject_id
GROUP BY s.student_id , s.name
HAVING COUNT(DISTINCT su.subject_name) = 4;
-- view all tables
select * from Student;
select * from Subject;
select * from student_subject_relationship;