-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuerySafari.sql
More file actions
335 lines (278 loc) · 10.7 KB
/
SQLQuerySafari.sql
File metadata and controls
335 lines (278 loc) · 10.7 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
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
334
335
-- Table creation
CREATE TABLE Animal (
Animal_ID CHAR(10) NOT NULL PRIMARY KEY,
CONSTRAINT check_Animal_ID CHECK (Animal_ID LIKE '%[0-9]%'),
Species VARCHAR(50) NULL,
CONSTRAINT check_species CHECK (Species LIKE '%[A-Z]%' AND Species LIKE '%[a-z]%'),
Name VARCHAR(50) NULL
);
CREATE TABLE Mammal_Predator (
Mammal_pre_ID CHAR(10) NOT NULL,
constraint FK__Mammal_Predator__Animal_ID FOREIGN KEY (Mammal_pre_ID) REFERENCES Animal(Animal_ID),
meatType VARCHAR(50)
);
CREATE TABLE Mammal_Vegetarian (
Mammal_veg_ID CHAR(10) NOT NULL,
constraint FK__Mammal_Vegetarian__Animal_ID FOREIGN KEY (Mammal_veg_ID) REFERENCES Animal(Animal_ID),
vegType VARCHAR(50)
);
CREATE TABLE Reptiles (
reptiles_ID CHAR(10) NOT NULL,
constraint FK__Reptiles__Animal_ID FOREIGN KEY (reptiles_ID) REFERENCES Animal(Animal_ID),
FoodType VARCHAR(50)
);
CREATE TABLE Attraction (
attrac_num CHAR(10) NOT NULL PRIMARY KEY,
CONSTRAINT attrac_num_check CHECK (attrac_num LIKE '%[0-9]%'), -- checks every char is 0-9
name VARCHAR(50) NULL
);
CREATE TABLE Restaurant (
attrac_num CHAR(10) NOT NULL primary key,
MilkOrMeat char(4) not null ,
constraint FK__Restaurant__Attraction FOREIGN KEY (attrac_num) REFERENCES Attraction(attrac_num),
CONSTRAINT MilkOrMeat_check CHECK (MilkOrMeat IN ('Milk', 'Meat'))
);
CREATE TABLE Employee (
First_name VARCHAR(50) NULL,
Second_name VARCHAR(50) NULL,
Employee_ID_num CHAR(9) NOT NULL PRIMARY KEY,
CONSTRAINT Employee_ID_num CHECK (Employee_ID_num LIKE '%[0-9]%') -- checks every char is 0-9
);
CREATE TABLE CareTaker (
CareTaker_ID_num CHAR(9) NOT NULL,
constraint FK__CareTaker__Employee_ID_num FOREIGN KEY (CareTaker_ID_num) REFERENCES Employee(Employee_ID_num),
exper INT DEFAULT 0 NOT NULL,
CONSTRAINT Experience_check CHECK (exper BETWEEN 0 AND 100) -- check is making sure the value between 0-100
);
CREATE TABLE Service (
service_ID_num CHAR(9) NOT NULL,
constraint FK__Service__Employee_ID_num FOREIGN KEY (service_ID_num) REFERENCES Employee(Employee_ID_num)
);
CREATE TABLE Customer (
cust_ID CHAR(10) NOT NULL PRIMARY KEY,
CONSTRAINT cust_ID CHECK (cust_ID LIKE '%[0-9]%'),
cust_age INT NULL,
CONSTRAINT cust_age_check CHECK (cust_age >= 0),
first_name VARCHAR(50) NULL,
last_name VARCHAR(50) NULL
);
create table Watches(
cust_ID CHAR(10) NOT NULL,
attrac_num CHAR(10) NOT NULL,
constraint FK__Watches__Customer_ID FOREIGN KEY (cust_ID) REFERENCES Customer(cust_ID),
constraint FK__Watches__Attraction FOREIGN KEY (attrac_num) REFERENCES Attraction(attrac_num)
);
CREATE table Feeding (
which_animal varchar(50) NULL,
attrac_num CHAR(10) NOT NULL primary key,
constraint FK__Feeding__Attraction FOREIGN KEY (attrac_num) REFERENCES Attraction(attrac_num)
);
CREATE table Participate (
cust_ID CHAR(10) NOT NULL,
attrac_num CHAR(10) NOT NULL,
constraint FK__Participate__Customer_ID FOREIGN KEY (cust_ID) REFERENCES Customer(cust_ID),
constraint FK__Participate__Attraction FOREIGN KEY (attrac_num) REFERENCES Attraction(attrac_num)
);
CREATE table Show (
ages int DEFAULT 0 NOT NULL,
attrac_num CHAR(10) NOT NULL primary key,
constraint FK__Show__Attraction FOREIGN KEY (attrac_num) REFERENCES Attraction(attrac_num)
);
-- Insert data into Employee table
INSERT INTO Employee (Employee_ID_num, First_name, Second_name) VALUES
('000000001', 'Jane', 'Doe'),
('000000002', 'Itay', 'Levi'),
('000000003', 'Taylor', 'Swift');
-- Insert data into CareTaker table
INSERT INTO CareTaker (CareTaker_ID_num, exper) VALUES
('000000001', 2);
-- Insert data into Service table
INSERT INTO Service (service_ID_num) VALUES
('000000002'),
('000000003');
-- Insert data into Customer table
INSERT INTO Customer (cust_ID, cust_age, first_name, last_name) VALUES
('000000005', 70, 'LeBron', 'James'),
('000000004', 22, 'Ravid', 'Plotnik'),
('000000006', 30 ,'Leo', 'Messi'),
('000000007', 2, 'Benny', 'Gantz'),
('000000008', 15, 'Yair', 'Lapid');
-- Insert data into Animal table
INSERT INTO Animal (Animal_ID, Species, Name) VALUES
('0000000001', 'Snake', 'Slinky'),
('0000000002', 'Lizard', 'Lizzy'),
('0000000003', 'Turtle', 'Shelly'),
('0000000004', 'Gecko', 'Gerry'),
('0000000005', 'Chameleon', 'Cammy'),
('1111111110','zebra','gigi'),
('1111111111','zebra','tuna'),
('1111111112','giraffe','shrek'),
('1111111113','giraffe','shahar'),
('1111111114','kengaroo','kengi');
-- Insert data into Reptiles table
INSERT INTO Reptiles (reptiles_ID, FoodType) VALUES
('0000000001', 'mouse'),
('0000000002', 'flower'),
('0000000003', 'flower'),
('0000000004', 'fly'),
('0000000005', 'fly');
INSERT INTO Mammal_Vegetarian (Mammal_veg_ID, vegType) VALUES
('1111111110','grass'),
('1111111111','grass'),
('1111111112','leaves'),
('1111111113','leaves'),
('1111111114','grass');
-- Insert data into Attraction table
INSERT INTO Attraction (attrac_num, name) VALUES
('2222222220', 'SNAKES FEEDING'),
('2222222221', 'LIONS FEEDING'),
('2222222222', 'KIDS SHOW'),
('2222222223', 'CLOWN'),
('2222222224', 'ELEPHANT FEEDING'),
('2222222225', 'ITALIAN SAFARI'),
('2222222226', 'CAFE JUNGLE');
INSERT INTO Feeding (attrac_num,which_animal) VALUES
('2222222220','SNAKE'),
('2222222221','LION'),
('2222222224','ELEPHANT');
INSERT INTO Restaurant(attrac_num, MilkOrMeat) VALUES
('2222222225', 'Meat'),
('2222222226', 'Milk');
INSERT INTO Show (ages,attrac_num) VALUES
(6,'2222222222'),
(18,'2222222223');
INSERT INTO Watches(cust_ID, attrac_num) VALUES
('000000005', '2222222220'),
('000000005', '2222222221'),
('000000006', '2222222220'),
('000000008', '2222222220'),
('000000006', '2222222224'),
('000000007', '2222222224');
INSERT INTO Participate(cust_ID, attrac_num) VALUES
('000000007', '2222222222'),
('000000008', '2222222222'),
('000000005', '2222222223'),
('000000007', '2222222223'),
('000000006', '2222222223');
--query 1
-- Select customers' age between 21 and 67
SELECT COUNT(cust_age) as ADULTS_CUST_COUNT
FROM Customer
WHERE cust_age BETWEEN 21 AND 67;
--query 2
-- Get animals that are giraffes or zebras
SELECT an.Species, an.Name
FROM Animal an
WHERE an.Species = 'Giraffe' OR an.Species = 'Zebra';
--query 3
--Gets list of the names of the customers that age is not allowed in a show
SELECT c.first_name, c.last_name, a.name
FROM Customer AS c
JOIN participate AS p ON c.cust_ID = p.cust_ID
JOIN show AS s ON p.attrac_num = s.attrac_num AND c.cust_age <= s.ages
JOIN Attraction AS a ON s.attrac_num = a.attrac_num ;
--query 4
--For every feeding show, the minimum age of people who came to watch
SELECT a.name AS Feeding_Show, MIN(c.cust_age) AS Min_Age
FROM watches w
JOIN Attraction a ON w.attrac_num = a.attrac_num
JOIN Customer c ON w.cust_ID = c.cust_ID
JOIN Feeding f ON a.attrac_num = f.attrac_num
GROUP BY a.name;
--query 5
-- Calculate the difference of each customer's age from the average age
SELECT c.cust_ID, c.first_name, c.last_name, c.cust_age,
(c.cust_age - (SELECT AVG(cust_age) FROM Customer)) AS age_diff_from_avg
INTO tempAvgAges
FROM Customer c;
-- Select from the temporary table
SELECT * FROM tempAvgAges;
drop table tempAvgAges;
--query 6
-- List of feeding show that have a greater number of watches than the average number of viewers for all feeding shows
WITH FeedingShowWatches AS (SELECT a.attrac_num, a.name, COUNT(w.cust_ID) AS watch_count
FROM watches w
JOIN Attraction a ON w.attrac_num = a.attrac_num
WHERE a.name LIKE '%FEEDING%'
GROUP BY a.attrac_num, a.name),
AverageWatches AS (SELECT AVG(watch_count) AS avg_watch_count
FROM FeedingShowWatches)
SELECT f.attrac_num, f.name, f.watch_count
INTO ShowsSummary
FROM FeedingShowWatches f
JOIN AverageWatches a ON f.watch_count > a.avg_watch_count;
SELECT * FROM ShowsSummary;
DROP TABLE ShowsSummary;
--query 7
-- For each show present the show serial number, and number of participants, only if the number of participants is greater than 2
select s.attrac_num, count(p.cust_ID) as Participant_count
from show as s join participate as p on s.attrac_num = p.attrac_num
group by s.attrac_num
having count(p.cust_ID) > 2
--query 8
-- Calculate the average age of customers for each show or feeding show
SELECT a.name AS Attraction_Name, AVG(c.cust_age) AS Avg_Age
FROM Customer c
JOIN (SELECT cust_ID, attrac_num FROM watches
UNION ALL
SELECT cust_ID, attrac_num FROM participate) ca ON c.cust_ID = ca.cust_ID
JOIN Attraction a ON ca.attrac_num = a.attrac_num
GROUP BY a.name;
--query 9
-- Retrieve all clients ordered from smallest to biggest based on their customer ID
SELECT cust_ID, first_name, last_name, cust_age
FROM Customer
ORDER BY cust_age;
--query 10
-- Update query to switch reptile food from flower to grass
UPDATE Reptiles
SET FoodType = 'Grass'
WHERE FoodType = 'Flower';
--query 11
-- Update query to switch all meat restaurants to dairy
UPDATE RESTAURANT
SET MilkOrMeat = 'Milk'
WHERE MilkOrMeat = 'Meat';
--query 12
-- Delete query to remove all people with first name starting with 'I'
DELETE FROM Service
WHERE service_ID_num IN (SELECT service_ID_num FROM Employee);
DELETE FROM Employee
WHERE Employee_ID_num IN (SELECT Employee_ID_num FROM Service);
DELETE FROM Employee
WHERE First_name LIKE 'I%';
--query 13
-- Delete from Reptiles table where species is Lizard
DELETE FROM Reptiles
WHERE reptiles_ID IN
(SELECT Animal_ID
FROM Animal
WHERE Species = 'Lizard');
-- Drop foreign key constraints
ALTER TABLE Mammal_Predator DROP CONSTRAINT FK__Mammal_Predator__Animal_ID;
ALTER TABLE Mammal_Vegetarian DROP CONSTRAINT FK__Mammal_Vegetarian__Animal_ID;
ALTER TABLE Reptiles DROP CONSTRAINT FK__Reptiles__Animal_ID;
ALTER TABLE CareTaker DROP CONSTRAINT FK__CareTaker__Employee_ID_num;
ALTER TABLE Service DROP CONSTRAINT FK__Service__Employee_ID_num;
ALTER TABLE Restaurant DROP CONSTRAINT FK__Restaurant__Attraction;
ALTER TABLE Watches DROP CONSTRAINT FK__Watches__Customer_ID;
ALTER TABLE Watches DROP CONSTRAINT FK__Watches__Attraction;
ALTER TABLE Participate DROP CONSTRAINT FK__Participate__Customer_ID;
ALTER TABLE Participate DROP CONSTRAINT FK__Participate__Attraction;
ALTER TABLE Feeding DROP CONSTRAINT FK__Feeding__Attraction;
ALTER TABLE Show DROP CONSTRAINT FK__Show__Attraction;
-- Drop tables
DROP TABLE Mammal_Predator;
DROP TABLE Mammal_Vegetarian;
DROP TABLE Reptiles;
DROP TABLE CareTaker;
DROP TABLE Service;
DROP TABLE Restaurant;
DROP TABLE Watches;
DROP TABLE Participate;
DROP TABLE Feeding;
DROP TABLE Show;
DROP TABLE Employee;
DROP TABLE Customer;
DROP TABLE Animal;
DROP TABLE Attraction;