-
Notifications
You must be signed in to change notification settings - Fork 17
/
ILS_DB_code.sql
333 lines (249 loc) · 8.8 KB
/
ILS_DB_code.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
-- Creating the tables --
CREATE TABLE teacher (
teacher_id INT PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
language_1 VARCHAR(3) NOT NULL,
language_2 VARCHAR(3),
dob DATE,
tax_id INT UNIQUE,
phone_no VARCHAR(20)
);
CREATE TABLE client (
client_id INT PRIMARY KEY,
client_name VARCHAR(40) NOT NULL,
address VARCHAR(60) NOT NULL,
industry VARCHAR(20)
);
CREATE TABLE participant (
participant_id INT PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
phone_no VARCHAR(20),
client INT
);
CREATE TABLE course (
course_id INT PRIMARY KEY,
course_name VARCHAR(40) NOT NULL,
language VARCHAR(3) NOT NULL,
level VARCHAR(2),
course_length_weeks INT,
start_date DATE,
in_school BOOLEAN,
teacher INT,
client INT
);
-- Altering the tables to establish the relationships via FOREIGN KEYs
ALTER TABLE participant
ADD FOREIGN KEY(client)
REFERENCES client(client_id)
ON DELETE SET NULL;
ALTER TABLE course
ADD FOREIGN KEY(teacher)
REFERENCES teacher(teacher_id)
ON DELETE SET NULL;
ALTER TABLE course
ADD FOREIGN KEY(client)
REFERENCES client(client_id)
ON DELETE SET NULL;
CREATE TABLE takes_course (
participant_id INT,
course_id INT,
PRIMARY KEY(participant_id, course_id),
FOREIGN KEY(participant_id) REFERENCES participant(participant_id) ON DELETE CASCADE, -- it makes no sense to keep this rtelation when a participant or course is no longer in the system, hence why CASCADE this time
FOREIGN KEY(course_id) REFERENCES course(course_id) ON DELETE CASCADE
);
/*
-- If needed to quickly delete and re-build the tables:
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE client;
DROP TABLE course;
DROP TABLE participant;
DROP TABLE takes_course;
DROP TABLE teacher;
SET FOREIGN_KEY_CHECKS = 1;
*/
-- Populating the tables
INSERT INTO teacher VALUES
(1, 'James', 'Smith', 'ENG', NULL, '1985-04-20', 12345, '+491774553676'),
(2, 'Stefanie', 'Martin', 'FRA', NULL, '1970-02-17', 23456, '+491234567890'),
(3, 'Steve', 'Wang', 'MAN', 'ENG', '1990-11-12', 34567, '+447840921333'),
(4, 'Friederike', 'Müller-Rossi', 'DEU', 'ITA', '1987-07-07', 45678, '+492345678901'),
(5, 'Isobel', 'Ivanova', 'RUS', 'ENG', '1963-05-30', 56789, '+491772635467'),
(6, 'Niamh', 'Murphy', 'ENG', 'IRI', '1995-09-08', 67890, '+491231231232');
INSERT INTO client VALUES
(101, 'Big Business Federation', '123 Falschungstraße, 10999 Berlin', 'NGO'),
(102, 'eCommerce GmbH', '27 Ersatz Allee, 10317 Berlin', 'Retail'),
(103, 'AutoMaker AG', '20 Künstlichstraße, 10023 Berlin', 'Auto'),
(104, 'Banko Bank', '12 Betrugstraße, 12345 Berlin', 'Banking'),
(105, 'WeMoveIt GmbH', '138 Arglistweg, 10065 Berlin', 'Logistics');
INSERT INTO participant VALUES
(101, 'Marina', 'Berg','491635558182', 101),
(102, 'Andrea', 'Duerr', '49159555740', 101),
(103, 'Philipp', 'Probst', '49155555692', 102),
(104, 'René', 'Brandt', '4916355546', 102),
(105, 'Susanne', 'Shuster', '49155555779', 102),
(106, 'Christian', 'Schreiner', '49162555375', 101),
(107, 'Harry', 'Kim', '49177555633', 101),
(108, 'Jan', 'Nowak', '49151555824', 101),
(109, 'Pablo', 'Garcia', '49162555176', 101),
(110, 'Melanie', 'Dreschler', '49151555527', 103),
(111, 'Dieter', 'Durr', '49178555311', 103),
(112, 'Max', 'Mustermann', '49152555195', 104),
(113, 'Maxine', 'Mustermann', '49177555355', 104),
(114, 'Heiko', 'Fleischer', '49155555581', 105);
INSERT INTO course VALUES
(12, 'English for Logistics', 'ENG', 'A1', 10, '2020-02-01', TRUE, 1, 105),
(13, 'Beginner English', 'ENG', 'A2', 40, '2019-11-12', FALSE, 6, 101),
(14, 'Intermediate English', 'ENG', 'B2', 40, '2019-11-12', FALSE, 6, 101),
(15, 'Advanced English', 'ENG', 'C1', 40, '2019-11-12', FALSE, 6, 101),
(16, 'Mandarin für Autoindustrie', 'MAN', 'B1', 15, '2020-01-15', TRUE, 3, 103),
(17, 'Français intermédiaire', 'FRA', 'B1', 18, '2020-04-03', FALSE, 2, 101),
(18, 'Deutsch für Anfänger', 'DEU', 'A2', 8, '2020-02-14', TRUE, 4, 102),
(19, 'Intermediate English', 'ENG', 'B2', 10, '2020-03-29', FALSE, 1, 104),
(20, 'Fortgeschrittenes Russisch', 'RUS', 'C1', 4, '2020-04-08', FALSE, 5, 103);
INSERT INTO takes_course VALUES
(101, 15),
(101, 17),
(102, 17),
(103, 18),
(104, 18),
(105, 18),
(106, 13),
(107, 13),
(108, 13),
(109, 14),
(109, 15),
(110, 16),
(110, 20),
(111, 16),
(114, 12),
(112, 19),
(113, 19);
-- ---------------------------------------------------------------------------------------------------------
-- Now for some fun! It's query-time.
-- Get all the details from the teachers table.
SELECT *
FROM teacher;
-- Get the last names and birthdays of all the teachers.
SELECT last_name, dob
FROM teacher;
-- Find all courses in English
SELECT *
FROM course
WHERE language = 'ENG';
SELECT *
FROM course
WHERE language = 'ENG'
ORDER BY start_date DESC;
-- Find all courses in English at B2 level
SELECT *
FROM course
WHERE language = 'ENG' AND level = 'B2';
-- Find all courses in English and all courses at B2 level
SELECT *
FROM course
WHERE language = 'ENG' OR level = 'C1';
-- Find all courses where the language is not English and the level is not C1
SELECT *
FROM course
WHERE NOT language = 'ENG' OR level = 'C1';
-- Get the names and phone numbers of all teachers born before 1990.
SELECT first_name, last_name, phone_no
FROM teacher
WHERE dob < '1990-01-01';
-- Do the same as before but use aliasing.
SELECT first_name AS First Name, last_name AS 'Last Name', phone_no AS Telephone
FROM teacher
WHERE dob < '1990-01-01';
-- Find all courses which start in January
SELECT *
FROM course
WHERE start_date BETWEEN '2020-01-01' AND '2020-01-31';
-- Find some intermediate courses
SELECT course_name
FROM course
WHERE course_name LIKE '%interm%';
-- Find participants whose last names are Garcia, Nowak or Mustermann
SELECT first_name, last_name
FROM participant
WHERE last_name IN ('Garcia', 'Nowak', 'Mustermann');
-- Identify teachers who only teach one or two languages
SELECT *
FROM teacher
WHERE language_2 IS NULL;
SELECT *
FROM teacher
WHERE language_2 IS NOT NULL;
-- Find the average length of a course
SELECT AVG(course_length_weeks)
FROM course;
-- Find the average length of a course, grouped by client
SELECT client, AVG(course_length_weeks)
FROM course
GROUP BY client;
-- Count the number of courses
SELECT COUNT(*)
FROM course;
-- Count the number of courses in English
SELECT COUNT(*)
FROM course
WHERE language = 'Eng';
-- How many courses does the school offer in each language?
SELECT language, COUNT(language)
FROM course
GROUP BY language;
-- Find all the teachers whose birthdate is above the average teacher's birthdate
SELECT *
FROM teacher
WHERE dob >
(SELECT AVG(dob)
FROM teacher);
-- Get the course details for all courses which take place at the client's offices.
SELECT course.course_id, course.course_name, course.language, client.client_name, client.address
FROM course
JOIN client
ON course.client = client.client_id
WHERE course.in_school = FALSE;
-- Get the course details for all courses which take place at the client's offices, which are taught
-- by Stefanie Martin (as above, but with added details).
SELECT course.course_id, course.course_name, course.language, client.client_name, client.address
FROM course
JOIN client
ON course.client = client.client_id
WHERE course.in_school = FALSE AND course.teacher = 2;
-- Get all particpants in classes taught by Niamh Murphy
SELECT participant.first_name, participant.last_name
FROM participant
JOIN takes_course ON takes_course.participant_id = participant.participant_id
JOIN course ON takes_course.course_id = course.course_id
WHERE takes_course.course_id =
(SELECT takes_course.course_id
WHERE course.teacher = 6);
-- NEW TABLE ALERT!
CREATE TABLE industry_prospects (
industry VARCHAR(20) PRIMARY KEY,
outlook VARCHAR(20)
);
INSERT INTO industry_prospects VALUES
('Retail', 'Good'),
('Hospitality', 'Poor'),
('Logistics', 'Terrible'),
('Tourism', 'Great'),
('Events', 'Good');
SELECT client.client_name, client.industry, industry_prospects.outlook
FROM client
JOIN industry_prospects
ON client.industry = industry_prospects.industry;
SELECT client.client_name, client.industry, industry_prospects.outlook
FROM client
LEFT JOIN industry_prospects
ON client.industry = industry_prospects.industry;
SELECT client.client_name, client.industry, industry_prospects.outlook
FROM client
RIGHT JOIN industry_prospects
ON client.industry = industry_prospects.industry;
SELECT client.client_name, client.industry, industry_prospects.outlook
FROM client
FULL JOIN industry_prospects
ON client.industry = industry_prospects.industry;