-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-6.sql
44 lines (34 loc) · 1.02 KB
/
query-6.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
/* The identifiers of the albums containing songs that are all by a single singer and containing at least three songs from years preceding the album's year. */
-- Lists all the albums with only one singer
SELECT
"Album"
FROM (
-- Lists all the singers per album
SELECT
SONG_SINGERS.PersonName AS "Singer",
ALBUM_SONGS.AlbumId AS "Album"
FROM
ALBUM_SONGS
INNER JOIN SONGS ON ALBUM_SONGS.SongId = SONGS.SongId
INNER JOIN SONG_SINGERS ON SONGS.SongId = SONG_SINGERS.SongId
GROUP BY
SONG_SINGERS.PersonName, ALBUM_SONGS.AlbumId
)
GROUP BY
"Album"
HAVING
COUNT("Singer") = 1
INTERSECT
-- Lists all the albums that have at least 3 songs which has been published before the album
SELECT
ALBUM_SONGS.AlbumId AS "Album"
FROM
ALBUM_SONGS
INNER JOIN SONGS ON ALBUM_SONGS.SongId = SONGS.SongId
INNER JOIN ALBUMS ON ALBUM_SONGS.AlbumId = ALBUMS.AlbumId
WHERE
SONGS.SongYear < ALBUMS.AlbumYear
GROUP BY
ALBUM_SONGS.AlbumId
HAVING
COUNT(*) >= 3;