-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvance SQL Project spotify.sql
More file actions
204 lines (130 loc) · 4.17 KB
/
Copy pathAdvance SQL Project spotify.sql
File metadata and controls
204 lines (130 loc) · 4.17 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
-- Advance SQL Project -- Spotify Datasets
-- create table
DROP TABLE IF EXISTS spotify;
CREATE TABLE 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)
);
-- EDA --
SELECT *
FROM spotify
SELECT COUNT (DISTINCT artist) FROM spotify;
SELECT DISTINCT album_type FROM spotify;
SELECT MAX (duration_min) FROM spotify;
SELECT MIN (duration_min) FROM spotify;
SELECT * FROM spotify
WHERE duration_min = 0
DELETE FROM spotify
WHERE duration_min = 0
SELECT * FROM spotify
WHERE duration_min = 0
SELECT DISTINCT channel FROM spotify;
-- -------------------------------
-- Data Analysis - Easy Category
-- -------------------------------
-- 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 album
-- 3. Get the total number of comments for tracks where `licensed = TRUE`.
SELECT SUM(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)
FROM spotify
GROUP BY artist
-- -------------------------------
-- Data Analysis - Medium Category
-- -------------------------------
-- 1. Calculate the average danceability of tracks in each album.
SELECT album , AVG(danceability) AS Avg_danceability
FROM spotify
GROUP BY album
-- 2. Find the top 5 tracks with the highest energy values.
SELECT track, energy
FROM spotify
GROUP BY track, energy
ORDER BY energy DESC LIMIT 5;
-- 3. List all tracks along with their total views and total likes where `official_video = TRUE`.
SELECT track, SUM(views) AS total_views, SUM(likes) total_likes
FROM spotify
WHERE official_video = 'true'
GROUP BY track
ORDER BY 2 DESC
LIMIT 5;
-- 4. For each album, calculate the total views of all associated tracks.
SELECT album , track ,SUM(views) AS total_album_views
FROM spotify
GROUP BY album, track
ORDER BY total_album_views DESC
-- 5. 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
) AS t1
WHERE streamed_on_spotify > streamed_on_youtube
AND streamed_on_youtube <> 0;
-- -------------------------------
-- Data Analysis - Hard Category
-- -------------------------------
--1. Find the top 3 most-viewed tracks for each artist using window fu nctions.
WITH ranking_artist AS
(SELECT artist , track, SUM(views) AS total_views,
DENSE_RANK() OVER(PARTITION BY artist ORDER BY SUM (views)DESC) as rank
FROM spotify
GROUP BY artist, track
ORDER BY artist, total_views DESC
)
SELECT * FROM ranking_artist
WHERE rank <= 3
FROM spotify
--2. Write a query to find tracks where the liveness score is above the average.
SELECT track, artist, liveness
FROM spotify
WHERE liveness > (SELECT AVG(liveness)FROM spotify)
--3. Use a `WITH` clause to calculate the difference between the
--highest and lowest energy values for tracks in each album.
WITH cte
AS
(SELECT album, MAX(energy) AS highest_energy, MIN(energy) AS lowest_energy
FROM spotify
GROUP BY album
)
SELECT album, highest_energy - lowest_energy AS energy_diff
FROM cte
ORDER BY energy_diff DESC