-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotify_SQL_query.sql
More file actions
151 lines (118 loc) · 3.56 KB
/
Copy pathSpotify_SQL_query.sql
File metadata and controls
151 lines (118 loc) · 3.56 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
-- Advanced SQL Project -- Spotify Datasets
DROP TABLE IF EXISTS spotify;
CREATE TABLE spotify(
artist VARCHAR (256),
track VARCHAR (256),
album VARCHAR (256),
album_type VARCHAR (50),
danceability FLOAT,
energy FLOAT,
loudness FLOAT,
speechness FLOAT,
acousticness FLOAT,
instrumentalness FLOAT,
liveness FLOAT,
valence FLOAT,
tempo FLOAT,
duration_min FLOAT,
title VARCHAR(256),
channel VARCHAR(256),
views BIGINT,
likes BIGINT,
comments BIGINT,
licensed BOOLEAN,
official_video BOOLEAN,
stream BIGINT,
energy_liveness FLOAT,
most_playedon VARCHAR(50));
--EDA--
SELECT * FROM spotify;
SELECT duration_min from spotify
WHERE duration_min =0;
-- delete these two songs with 0 duration---
DELETE FROM spotify
WHERE duration_min = 0;
--------------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 respectives artists.*/
SELECT DISTINCT album, artist FROM spotify;
/* 3. Get the total numbers of comments for the tracks where licensed = True*/
SELECT
sum (comments) as total_cooments 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(*) from spotify
GROUP BY artist
/* 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, energy
FROM spotify
ORDER BY 2 desc
limit 5
/* 8. List all the tracks along 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) as total_views
from spotify
group by 1,2
order by 3 desc
/* 10. Retrieve the track names that have been streamed on spotify more than youtube. */
SELECT *
FROM (
SELECT
track,
COALESCE(SUM(CASE WHEN most_playedon = 'Youtube' THEN stream END), 0) AS streamed_on_youtube,
COALESCE(SUM(CASE WHEN most_playedon = '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;
/* 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 rank
FROM spotify
GROUP BY 1,2
ORDER BY 1,3 DESC)
SELECT * FROM ranking_artist
WHERE rank <=3
/* 12. Write a query to find tracks where the liveness score is above the average.*/
SELECT 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 cte AS (
SELECT
album,
MAX(energy) AS maximum_energy,
MIN(energy) AS minimum_energy
FROM spotify
GROUP BY album
)
SELECT
album,
maximum_energy - minimum_energy AS energy_diff
FROM cte
ORDER BY energy_diff DESC;