Skip to content

Commit 90358a9

Browse files
committed
few correct solutions
1 parent b5ce0a7 commit 90358a9

File tree

3 files changed

+107
-32
lines changed

3 files changed

+107
-32
lines changed

SQL Academy/Practice.sql SQL-Academy/Practice.sql

+30-30
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ FROM Trip AS t
7070
JOIN Pass_in_trip AS pt ON pt.trip=t.id
7171
JOIN Passenger AS p ON p.id=pt.passenger
7272
AND p.name='Bruce Willis'
73-
GROUP BY t.town_to
73+
GROUP BY t.town_to;
7474

7575
-- Exercise № 15
7676
SELECT DISTINCT Trip.time_in
7777
FROM Trip, Passenger
7878
WHERE Passenger.name='Steve Martin' AND town_to='London'
79-
GROUP BY time_in LIMIT 1
79+
GROUP BY time_in LIMIT 1;
8080

8181
-- Exercise № 16
8282
SELECT p.name, COUNT(pt.trip) AS count
@@ -113,7 +113,7 @@ JOIN Goods AS g ON g.good_id=p.good
113113
JOIN GoodTypes AS gt ON gt.good_type_id=g.type
114114
WHERE gt.good_type_name='entertainment'
115115
GROUP BY fm.status, fm.member_name
116-
ORDER BY costs
116+
ORDER BY costs;
117117

118118
-- Exercise № 21
119119
SELECT g.good_name
@@ -135,7 +135,7 @@ JOIN GoodTypes AS gt ON gt.good_type_id=g.type
135135
WHERE gt.good_type_name='delicacies'
136136
GROUP BY g.good_name, p.unit_price
137137
ORDER BY p.unit_price DESC LIMIT 1;
138-
//-----
138+
--
139139
SELECT good_name, unit_price
140140
FROM Payments, Goods, GoodTypes
141141
WHERE good_type_id=type
@@ -153,15 +153,15 @@ SELECT fm.member_name, SUM(p.amount*p.unit_price) as costs
153153
FROM FamilyMembers AS fm
154154
JOIN Payments AS p ON p.family_member=fm.member_id
155155
WHERE Year(p.date)=2005 AND MONTH(p.date)=6
156-
GROUP BY fm.member_name
156+
GROUP BY fm.member_name;
157157

158158
-- Exercise № 25
159159
SELECT g.good_name
160160
FROM Goods AS g
161161
LEFT JOIN Payments AS p ON g.good_id=p.good AND YEAR(p.date)=2005
162162
WHERE p.good IS NULL
163-
GROUP BY g.good_id
164-
//-----
163+
GROUP BY g.good_id;
164+
--
165165
SELECT g.good_name FROM Goods AS g
166166
WHERE NOT EXISTS
167167
(SELECT good FROM Payments AS p
@@ -197,13 +197,13 @@ WHERE p.id IN
197197
FROM Passenger AS p
198198
JOIN Pass_in_trip AS pt ON p.id=pt.passenger
199199
JOIN Trip AS t ON t.id=pt.trip
200-
WHERE t.town_to='Moscow' AND t.plane='TU-134')
200+
WHERE t.town_to='Moscow' AND t.plane='TU-134');
201201

202202
-- Exercise № 30
203203
SELECT pt.trip, COUNT(pt.passenger) AS count
204204
FROM Pass_in_trip AS pt
205205
GROUP BY pt.trip
206-
ORDER BY count DESC
206+
ORDER BY count DESC;
207207

208208
-- Exercise № 31
209209
SELECT *
@@ -212,13 +212,13 @@ WHERE member_name LIKE '%Quincey'
212212

213213
-- Exercise № 32
214214
SELECT FLOOR(AVG((YEAR(CURRENT_DATE)-YEAR(birthday))-(RIGHT(CURRENT_DATE,5)<RIGHT(birthday,5)))) AS age
215-
FROM FamilyMembers
215+
FROM FamilyMembers;
216216

217217
-- Exercise № 33
218218
SELECT AVG(p.unit_price) as cost
219219
FROM Payments AS p
220220
JOIN Goods AS g On g.good_id=p.good
221-
WHERE g.good_name LIKE '%caviar'
221+
WHERE g.good_name LIKE '%caviar';
222222

223223
-- Exercise № 34
224224
SELECT COUNT(c.name) as count
@@ -242,7 +242,7 @@ WHERE student.address like 'ul. Pushkina%';
242242

243243
-- Exercise № 37
244244
SELECT Min((YEAR(CURRENT_DATE)-YEAR(birthday))-(RIGHT(CURRENT_DATE,5)<RIGHT(birthday,5))) AS year
245-
FROM Student
245+
FROM Student;
246246

247247
-- Exercise № 38
248248
SELECT COUNT(s.first_name) as count
@@ -260,7 +260,7 @@ SELECT sub.name as subjects
260260
FROM Subject AS sub
261261
JOIN Schedule AS s ON s.subject=sub.id
262262
JOIN Teacher AS t ON t.id=s.teacher
263-
WHERE t.last_name LIKE 'Romashkin'
263+
WHERE t.last_name LIKE 'Romashkin';
264264

265265
-- Exercise № 41
266266
SELECT tp.start_pair
@@ -275,15 +275,15 @@ SELECT TIMEDIFF(
275275
WHERE id=4),
276276
(SELECT start_pair
277277
FROM Timepair
278-
WHERE id=2)) AS time
278+
WHERE id=2)) AS time;
279279

280280
-- Exercise № 43
281281
SELECT t.last_name
282282
FROM Teacher as t
283283
JOIN Schedule AS s ON s.teacher=t.id
284284
JOIN Subject AS sub ON sub.id=s.subject
285285
WHERE sub.name LIKE 'Physical Culture'
286-
ORDER BY t.last_name
286+
ORDER BY t.last_name;
287287

288288
-- Exercise № 46
289289
SELECT c.name
@@ -293,20 +293,20 @@ WHERE c.id IN
293293
FROM Class AS c
294294
JOIN Schedule AS s ON s.class=c.id
295295
JOIN Teacher AS t ON t.id=s.teacher
296-
WHERE t.last_name LIKE 'Krauze')
296+
WHERE t.last_name LIKE 'Krauze');
297297

298298
-- Exercise № 47
299299
SELECT COUNT(s.number_pair) as count
300300
FROM Schedule AS s
301301
JOIN Teacher AS t ON t.id=s.teacher
302-
WHERE t.last_name='Krauze' AND s.date Like '2019-08-30'
302+
WHERE t.last_name='Krauze' AND s.date Like '2019-08-30';
303303

304304
-- Exercise № 48
305305
SELECT c.name, COUNT(sc.class) AS count
306306
FROM Class AS c
307307
JOIN Student_in_class AS sc ON sc.class=c.id
308308
GROUP BY c.name, sc.class
309-
ORDER BY count DESC
309+
ORDER BY count DESC;
310310

311311
-- Exercise № 49
312312
SELECT
@@ -316,7 +316,7 @@ SELECT
316316
ON class=Class.id
317317
WHERE Class.name='10 A') /
318318
(SELECT COUNT(*)
319-
FROM Student_in_class) * 100 AS percent
319+
FROM Student_in_class) * 100 AS percent;
320320

321321
-- Exercise № 50
322322
SELECT
@@ -325,45 +325,45 @@ SELECT
325325
JOIN Student_in_class AS sc ON s.id=sc.student
326326
WHERE YEAR(s.birthday)=2000) /
327327
(SELECT COUNT(*)
328-
FROM Student) * 100) AS percent
328+
FROM Student) * 100) AS percent;
329329

330330
-- Exercise № 51
331331
INSERT INTO Goods
332-
SELECT COUNT(*) + 1, 'Cheese', 2 FROM Goods
332+
SELECT COUNT(*) + 1, 'Cheese', 2 FROM Goods;
333333

334334
-- Exercise № 52
335335
INSERT INTO GoodTypes
336-
SELECT COUNT(*) + 1, 'auto' FROM GoodTypes
336+
SELECT COUNT(*) + 1, 'auto' FROM GoodTypes;
337337

338338
-- Exercise № 53
339339
UPDATE FamilyMembers
340340
SET member_name = "Andie Anthony"
341-
WHERE member_name = "Andie Quincey"
341+
WHERE member_name = "Andie Quincey";
342342

343343
-- Exercise № 54
344344
DELETE FROM FamilyMembers
345-
WHERE member_name LIKE '%Quincey'
345+
WHERE member_name LIKE '%Quincey';
346346

347347
-- Exercise № 56
348348
DELETE FROM Trip
349-
WHERE Trip.town_from='Moscow'
349+
WHERE Trip.town_from='Moscow';
350350

351351
-- Exercise № 57
352352
UPDATE Timepair
353353
SET start_pair=(start_pair+INTERVAL 30 MINUTE),
354-
end_pair=(end_pair+INTERVAL 30 MINUTE)
355-
//-----
354+
end_pair=(end_pair+INTERVAL 30 MINUTE);
355+
--
356356
UPDATE Timepair
357357
SET start_pair=ADDTIME(start_pair,
358-
'00:30:00'), end_pair=ADDTIME(end_pair, '00:30:00')
358+
'00:30:00'), end_pair=ADDTIME(end_pair, '00:30:00');
359359

360360
-- Exercise № 59
361361
SELECT *
362362
FROM Users
363-
WHERE phone_number LIKE '+375%'
363+
WHERE phone_number LIKE '+375%';
364364

365365
-- Exercise № 65
366366
SELECT res.room_id, FLOOR(AVG(rev.rating)) AS rating
367367
FROM Reservations AS res
368368
JOIN Reviews AS rev ON res.id=rev.reservation_id
369-
GROUP BY res.room_id
369+
GROUP BY res.room_id;

SQL Bolt/Practice.sql SQL-Bolt/Practice.sql

+76-1
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,79 @@ Join boxoffice b on b.movie_id=m.id;
174174
-- List all movies that were released on even number years
175175
Select title
176176
From movies
177-
Where year%2=0;
177+
Where year%2=0;
178+
179+
--SQL Lesson 10: Queries with aggregates (Pt. 1)
180+
-- Find the longest time that an employee has been at the studio
181+
SELECT years_employed as year
182+
FROM employees
183+
Order by year desc
184+
Limit 1;
185+
186+
-- For each role, find the average number of years employed by employees in that role
187+
SELECT role, avg(years_employed) as year
188+
FROM employees
189+
Group by role;
190+
191+
-- Find the total number of employee years worked in each building
192+
SELECT building, sum(years_employed) as sum
193+
FROM employees
194+
Group by building;
195+
196+
-- SQL Lesson 11: Queries with aggregates (Pt. 2)
197+
-- Find the number of Artists in the studio (without a HAVING clause)
198+
SELECT Count(*)
199+
FROM employees
200+
Where role='Artist';
201+
202+
-- Find the number of Employees of each role in the studio
203+
SELECT role, Count(*)
204+
FROM employees
205+
Group by role;
206+
207+
-- Find the total number of years employed by all Engineers
208+
SELECT sum(years_employed) as sum
209+
FROM employees
210+
Where role='Engineer';
211+
212+
-- SQL Lesson 12: Order of execution of a Query
213+
-- Find the number of movies each director has directed
214+
SELECT director, Count(*)
215+
FROM movies
216+
Group by director;
217+
218+
-- Find the total domestic and international sales that can be attributed to each director
219+
SELECT
220+
m.director,
221+
sum(b.domestic_sales+b.international_sales) as sum
222+
FROM movies m
223+
Join boxoffice b on b.movie_id=m.id
224+
Group by m.director;
225+
226+
-- SQL Lesson 13: Inserting rows
227+
-- Add the studio's new production, Toy Story 4 to the list of movies (you can use any director)
228+
INSERT INTO movies
229+
(id, title, director)
230+
VALUES (15, 'Toy Story 4', 'John Lasseter');
231+
232+
-- Toy Story 4 has been released to critical acclaim! It had a rating of 8.7, and made 340 million domestically and 270 million internationally. Add the record to the BoxOffice table.
233+
INSERT INTO boxoffice
234+
(movie_id, rating, domestic_sales, international_sales)
235+
VALUES (15, 8.7, 340000000, 270000000);
236+
237+
-- SQL Lesson 14: Updating rows
238+
-- The director for A Bug's Life is incorrect, it was actually directed by John Lasseter
239+
UPDATE movies
240+
SET director='John Lasseter'
241+
WHERE title like '%Life';
242+
243+
-- The year that Toy Story 2 was released is incorrect, it was actually released in 1999
244+
UPDATE movies
245+
SET year=1999
246+
WHERE title='Toy Story 2';
247+
248+
-- Both the title and director for Toy Story 8 is incorrect! The title should be "Toy Story 3" and it was directed by Lee Unkrich
249+
UPDATE movies
250+
SET title='Toy Story 3',
251+
director='Lee Unkrich'
252+
WHERE title='Toy Story 8';

W3School/Practice.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Where Country = 'Norway';
8080

8181
-- Exercise 3
8282
Update Customers
83-
Set City = 'Oslo'
83+
Set City = 'Oslo',
8484
Country = 'Norway'
8585
WHERE CustomerID = 32;
8686

0 commit comments

Comments
 (0)