-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotify_SQL_Project.sql
More file actions
193 lines (157 loc) · 4.96 KB
/
Copy pathSpotify_SQL_Project.sql
File metadata and controls
193 lines (157 loc) · 4.96 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
--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 COUNT(*) FROM spotify;
SELECT COUNT(DISTINCT(artist)) FROM spotify;
SELECT COUNT(DISTINCT(album)) FROM spotify;
SELECT DISTINCT album_type FROM spotify;
SELECT MAX(duration_min) , MIN(duration_min) FROM spotify;
SELECT * FROM spotify
WHERE duration_min = 0;
DELETE FROM spotify
WHERE duration_min = 0;
SELECT DISTINCT channel FROM spotify;
SELECT DISTINCT most_played_on FROM spotify;
-------------------------------------------------------------------------
-- DATA ANALYSIS -- BEGINNER LEVEL
-------------------------------------------------------------------------
--Question 1) Retrieve the names of all tracks that have more than 1 billion streams.
SELECT * FROM spotify
WHERE stream > 1000000000;
--Question 2) List all albums along with their respective artists.
SELECT album, artist
from spotify;
SELECT
DISTINCT album ,
artist
FROM spotify
ORDER BY 1;
--Question 3) Get the total number of comments for tracks where licensed = TRUE.
SELECT
SUM(comments) AS total_Comments
FROM spotify
WHERE licensed = 'true';
-- Question 4) Find all tracks that belong to the album type single.
SELECT * FROM spotify
WHERE album_type = 'single';
--Question 5) Count the total number of tracks by each artist.
SELECT artist , COUNT(track)
FROM spotify
GROUP BY artist
ORDER BY count(track) DESC;
-------------------------------------------------------------------------
-- DATA ANALYSIS -- MODERATE LEVEL
-------------------------------------------------------------------------
-- Question 1) Calculate the average danceability of tracks in each album.
SELECT album,
AVG(danceability)
AS avg_danceability
FROM spotify
GROUP BY album
ORDER BY AVG(danceability) DESC;
-- Question 2) Find the top 5 tracks with the highest energy values.
SELECT track ,
energy
FROM spotify
ORDER BY energy DESC
LIMIT 5;
-- Question 3) 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 track
LIMIT 5;
--Question 4)For each album, calculate the total views of all associated tracks.
SELECT album,
track,
SUM(VIEWS) AS total_views
FROM spotify
GROUP BY album, track
ORDER BY SUM(views) DESC;
-- Question 5) Retrieve the track names that have been streamed on Spotify more than YouTube.
SELECT * FROM
(SELECT
track,
COALESCE(SUM(CASE WHEN LOWER(most_played_on) = 'youtube' THEN stream END), 0) AS streamed_on_youtube,
COALESCE(SUM(CASE WHEN LOWER(most_played_on) = 'spotify' THEN stream END), 0) AS streamed_on_spotify
FROM spotify
GROUP BY track) AS T1
WHERE
streamed_on_youtube > streamed_on_spotify
AND
streamed_on_spotify <> 0
-------------------------------------------------------------------------
-- DATA ANALYSIS -- ADVANCE LEVEL
-------------------------------------------------------------------------
-- Question 1) Find the top 3 most-viewed tracks for each artist using window functions.
WITH artist_ranking 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 1 , 2
ORDER BY 1 , 3 DESC)
SELECT * FROM artist_ranking
WHERE RANK <= 3
-- Question 2) 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);
--Question 3) Use a WITH clause to calculate the difference between the highest and lowest energy values for tracks in each album.
WITH find_diff AS (
SELECT
album,
MAX(energy) AS max_energy,
MIN(energy) AS min_energy
FROM spotify
GROUP BY album
)
SELECT
album,
ROUND(CAST(max_energy - min_energy AS numeric), 2) AS difference_in_energy
FROM find_diff
ORDER BY 2 DESC;
--Question 4) Find tracks where the energy-to-liveness ratio is greater than 1.2.
SELECT track,
energy / liveness AS ratio_of_energy_to_liveness
FROM spotify
WHERE energy / liveness > 1.2;
--Question 5) Calculate the cumulative sum of likes for tracks ordered by the number of views, using window functions.
SELECT track,
views,
SUM(likes) OVER(ORDER BY views) AS cumulative_sum
FROM spotify
ORDER BY SUM(likes) OVER(ORDER BY views) DESC;