-
Notifications
You must be signed in to change notification settings - Fork 0
/
SELECT_requests.sql
98 lines (88 loc) · 3.63 KB
/
SELECT_requests.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
-- 1. Количество исполнителей в каждом жанре;
-- Способ 1
SELECT COUNT(performer_id) AS count_pi, genre_id AS gi
FROM performers_genres AS pg
GROUP BY gi
ORDER BY count_pi DESC;
-- Способ 2
SELECT COUNT(performer_id) AS count_performer, name_genre AS ng
FROM performers_genres AS pg
JOIN genres AS g
ON pg.genre_id = g.genre_id
GROUP BY ng
ORDER BY count_performer DESC;
-- 2. Количество треков, вошедших в альбомы 2019-2020 годов;
SELECT name_album, year_album, COUNT(name_track) AS count_nt
FROM albums AS a
JOIN tracks AS t
ON a.album_id = t.album_id
WHERE year_album BETWEEN 2019 AND 2020
GROUP BY a.album_id
ORDER BY count_nt DESC;
-- 3. Средняя продолжительность треков по каждому альбому;
SELECT name_album AS na, AVG(duration)
FROM tracks AS t
JOIN albums AS a
ON a.album_id = t.album_id
GROUP BY na;
-- 4. Все исполнители, которые не выпустили альбомы в 2020 году;
SELECT alias, name_album, year_album
FROM albums AS a
JOIN performers_albums AS pa
ON a.album_id = pa.album_id
JOIN performers AS p
ON p.performer_id = pa.performer_id
WHERE NOT year_album = 2020;
-- 5. Названия сборников, в которых присутствует конкретный исполнитель 'Frank Sinatra';
SELECT DISTINCT(name_collection), alias
FROM collections AS c
JOIN collections_tracks AS ct
ON c.collection_id = ct.collection_id
JOIN tracks AS t
ON ct.track_id = t.track_id
JOIN performers_albums AS pa
ON t.album_id = pa.album_id
JOIN performers AS p
ON pa.performer_id = p.performer_id
WHERE alias LIKE 'Frank Sinatra';
--6. Название альбомов, в которых присутствуют исполнители более 1 жанра;
SELECT name_album, COUNT(name_genre) AS count_genre
FROM albums AS a
JOIN performers_albums AS pa
ON a.album_id = pa.album_id
JOIN performers_genres AS pg
ON pa.performer_id = pg.performer_id
JOIN genres AS g
ON pg.genre_id = g.genre_id
GROUP BY name_album
HAVING COUNT(name_genre) > 1;
--7. Наименование треков, которые не входят в сборники;
SELECT name_track
FROM tracks
WHERE name_track NOT IN
(SELECT DISTINCT(name_track)
FROM tracks AS t
JOIN collections_tracks AS ct
ON t.track_id = ct.track_id);
--8. Исполнителя(-ей), написавшего самый короткий по продолжительности трек;
SELECT alias, duration
FROM tracks AS t
JOIN performers_albums AS pa
ON t.album_id = pa.album_id
JOIN performers AS p
ON pa.performer_id = p.performer_id
WHERE duration = (SELECT MIN(duration)
FROM tracks)
--9. Названия альбомов, содержащих наименьшее количество треков.
SELECT name_album, COUNT(name_track) AS count_track
FROM albums AS a
JOIN tracks AS t
ON a.album_id = t.album_id
GROUP BY name_album
HAVING COUNT(name_track) = (SELECT COUNT(name_track)
FROM albums AS a
JOIN tracks AS t
ON a.album_id = t.album_id
GROUP BY name_album
ORDER BY COUNT(name_track)
LIMIT 1);