-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotify_pj.sql
More file actions
175 lines (130 loc) · 3.5 KB
/
Spotify_pj.sql
File metadata and controls
175 lines (130 loc) · 3.5 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
CREATE TABLE IF NOT EXISTS spotify (
artist VARCHAR(255),
track VARCHAR(255),
album VARCHAR(255),
album_type VARCHAR(50),
danceability FLOAT,
energy FLOAT,
loudness FLOAT,
speechiness FLOAT,
acousticness FLOAT,
instrumentalness FLOAT,
liveness FLOAT,
valence FLOAT,
tempo FLOAT,
duration_min FLOAT,
title VARCHAR(255),
channel VARCHAR(255),
views FLOAT,
likes BIGINT,
comments BIGINT,
licensed BOOLEAN,
official_video BOOLEAN,
stream BIGINT,
energy_liveness FLOAT,
most_played_on VARCHAR(50)
);
select * from spotify;
SELECT * FROM spotify WHERE duration_min = 0;
DELETE FROM spotify WHERE duration_min = 0;
-- 1. Retrieve the names of all tracks that have more than 1 billion streams.
SELECT track
FROM spotify
WHERE stream > 1000000000;
-- 2. List all albums along with their respective artists.
SELECT
DISTINCT album, artist
FROM spotify
ORDER BY 1;
-- 3. Get the total number of comments for tracks where licensed = TRUE.
SELECT
sum(comments) as total_comments
FROM spotify
WHERE licensed = 'true';
-- 4. Find all tracks that belong to the album type single.
SELECT
track
FROM spotify
WHERE album_type = 'single';
-- 5. Count the total number of tracks by each artist.
SELECT
artist, count(track) AS no_of_tracks
FROM spotify
GROUP BY 1;
-- 6. Calculate the average danceability of tracks in each album.
SELECT
album,avg(danceability) AS avg_danceability
FROM spotify
GROUP BY 1
ORDER BY 2 DESC;
-- 7. Find the top 5 tracks with the highest energy values.
SELECT
track, MAX(energy) AS max_energy
FROM spotify
GROUP BY 1
ORDER BY 2 DESC
LIMIT 5;
-- 8. List all tracks along with their views and likes where official_video = TRUE.
SELECT
track,
SUM(views) AS total_views,
SUM(likes) AS total_likes
FROM spotify
WHERE official_video = 'true'
GROUP BY 1
ORDER BY 2 DESC;
-- 9. For each album, calculate the total views of all associated tracks.
select
album, track, SUM(views)
FROM spotify
GROUP BY 1,2
ORDER By 2;
-- 10. Retrieve the track names that have been streamed on Spotify more than YouTube.
SELECT *
FROM(
SELECT
track,
COALESCE (SUM(CASE WHEN most_played_on = 'Youtube' THEN stream END),0) AS streamed_on_youtube,
COALESCE (SUM(CASE WHEN most_played_on = 'Spotify' THEN stream END),0) AS streamed_on_spotify
FROM spotify
GROUP BY 1
ORDER BY 3 DESC) as t1
WHERE streamed_on_youtube<streamed_on_spotify
AND
streamed_on_spotify <> 0
AND
streamed_on_youtube <> 0;
-- 11. Find the top 3 most-viewed tracks for each artist using window functions.
WITH ranking_artist
AS
(SELECT
artist,
track,
SUM(views) AS total_views,
DENSE_RANK() OVER(PARTITION BY artist ORDER BY SUM(views) DESC) as ranking
FROM spotify
GROUP BY 1,2
ORDER BY 1,3 DESC)
SELECT *
FROM ranking_artist
WHERE ranking <=3;
-- 12. Write a query to find tracks where the liveness score is above the average.
SELECT
artist,
track,
liveness
FROM spotify
WHERE liveness > (SELECT AVG(liveness) FROM spotify)
ORDER BY liveness DESC;
-- 13. Use a WITH clause to calculate the difference between the highest and lowest energy values for tracks in each album.
WITH energy_diff
AS(SELECT
album,
MAX(energy) AS max_energy,
MIN(energy) AS min_energy
FROM spotify
GROUP BY 1)
SELECT *,
max_energy - min_energy as energy_diffrence
FROM energy_diff
ORDER BY album;