Skip to content

Kaushalkumar217/Spotify-Data-Analysis-Using-SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎡 Spotify Data Analysis Using SQL

Spotify SQL Project Banner


πŸ“Œ Project Overview

This project focuses on analyzing Spotify music data using SQL. It covers everything from basic data exploration to advanced SQL concepts such as CTEs, Window Functions, Aggregate Functions, Conditional Aggregation, and Subqueries.

The objective is to extract meaningful insights from Spotify datasets using SQL queries.


🎯 Objectives

  • 🎡 Explore Spotify music dataset
  • πŸ“Š Perform Exploratory Data Analysis (EDA)
  • πŸ” Answer real-world business questions
  • πŸ“ˆ Practice SQL from Beginner to Advanced
  • πŸ’‘ Learn Window Functions, CTEs and Aggregate Functions

πŸ› οΈ Tech Stack

  • PostgreSQL
  • SQL
  • Git
  • GitHub

πŸ“Š Dataset Information

The dataset contains information about Spotify tracks including:

  • 🎀 Artist
  • 🎡 Track
  • πŸ’Ώ Album
  • πŸ“€ Album Type
  • πŸ’ƒ Danceability
  • ⚑ Energy
  • πŸ”Š Loudness
  • πŸ—£ Speechiness
  • 🎼 Acousticness
  • 🎻 Instrumentalness
  • ❀️ Valence
  • 🎧 Liveness
  • ⏱ Tempo
  • βŒ› Duration
  • πŸ‘€ Views
  • πŸ‘ Likes
  • πŸ’¬ Comments
  • πŸ“ˆ Streams
  • β–Ά Platform (Spotify / YouTube)

πŸ” Exploratory Data Analysis (EDA)

πŸ“Œ Display complete dataset

SELECT *
FROM spotify;

πŸ“Œ Count total artists

SELECT COUNT(DISTINCT artist)
FROM spotify;

πŸ“Œ Find album types

SELECT DISTINCT album_type
FROM spotify;

πŸ“Œ Maximum track duration

SELECT MAX(duration_min)
FROM spotify;

πŸ“Œ Minimum track duration

SELECT MIN(duration_min)
FROM spotify;

πŸ“Œ Find tracks having 0 duration

SELECT *
FROM spotify
WHERE duration_min = 0;

πŸ“Œ Remove tracks having 0 duration

DELETE FROM spotify
WHERE duration_min = 0;

πŸ“Œ List all available channels

SELECT DISTINCT channel
FROM spotify;

🟒 Easy Level SQL Questions

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;

🟑 Medium Level SQL Questions

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 total views and total 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
ORDER BY total_views DESC
LIMIT 5;

4️⃣ Calculate total views of all tracks in each album.

SELECT album,
track,
SUM(views) AS total_album_views
FROM spotify
GROUP BY album,track
ORDER BY total_album_views DESC;

5️⃣ Retrieve track names streamed more on Spotify 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;

πŸ”΄ Hard Level SQL Questions

1️⃣ 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 artist,track
ORDER BY artist,total_views DESC
)

SELECT *
FROM ranking_artist
WHERE rank<=3;

2️⃣ Find tracks where the liveness score is above the average.

SELECT track,
artist,
liveness
FROM spotify
WHERE liveness >
(
SELECT AVG(liveness)
FROM spotify
);

3️⃣ Calculate the difference between the highest and lowest energy values for each album using CTE.

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;

πŸ“š SQL Concepts Covered

  • βœ… SELECT
  • βœ… WHERE
  • βœ… ORDER BY
  • βœ… GROUP BY
  • βœ… Aggregate Functions
  • βœ… DISTINCT
  • βœ… LIMIT
  • βœ… CASE WHEN
  • βœ… COALESCE
  • βœ… Subqueries
  • βœ… Common Table Expressions (CTE)
  • βœ… Window Functions
  • βœ… DENSE_RANK()
  • βœ… DELETE
  • βœ… Data Cleaning

πŸš€ Key Learnings

  • βœ” SQL Query Writing
  • βœ” Data Cleaning
  • βœ” Business Problem Solving
  • βœ” Data Aggregation
  • βœ” Window Functions
  • βœ” CTEs
  • βœ” Analytical Thinking

⭐ If you found this project useful

Please consider giving it a ⭐ on GitHub!


About

Spotify Data Analysis Project 🎡 Advanced SQL script to analyze Spotify datasets. It creates a structured spotify table with comprehensive attributes (artists, tracks, streams, energy, views, likes) and includes Exploratory Data Analysis (EDA) to uncover deep music trends and streaming insights.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors