What does a real analyst actually do with a database? They ask business questions - and then write the SQL to answer them precisely. This project does exactly that, across 11 progressively complex queries on a live music store schema.
SQL Music Store Analysis is a business intelligence project built entirely in SQL on a relational music store database. It covers three tiers of analytical complexity - from basic aggregations to advanced window functions and recursive CTEs - producing actionable insights on sales performance, customer behaviour, artist popularity, and market penetration by country.
This project demonstrates not just SQL syntax, but the analyst's mindset: translating business problems into queries, and queries into decisions.
The analysis runs on an 11-table relational schema covering the full music store lifecycle:
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β employee β β customer β β artist β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β employee_id β β customer_id β β artist_id β
β first_name β β first_name β β name β
β last_name β β last_name β ββββββββ¬ββββββββ
β title β β email β β
β levels β β country β ββββββββΌββββββββ
ββββββββββββββββ ββββββββ¬ββββββββ β album β
β ββββββββββββββββ
ββββββββΌββββββββ β album_id β
β invoice β β title β
ββββββββββββββββ β artist_id β
β invoice_id β ββββββββ¬ββββββββ
β customer_id β β
β billing_city β ββββββββΌββββββββ
β total β β track β
ββββββββ¬ββββββββ ββββββββββββββββ
β β track_id β
ββββββββΌββββββββ β name β
β invoice_line β β album_id β
ββββββββββββββββ β genre_id β
β invoice_id β β millisecondsβ
β track_id βββββββ unit_price β
β unit_price β ββββββββ¬ββββββββ
β quantity β β
ββββββββββββββββ ββββββββΌββββββββ
β genre β
ββββββββββββββββ
β genre_id β
β name β
ββββββββββββββββ
The queries are structured across three difficulty levels, each with increasing analytical complexity.
| # | Business Question | SQL Concepts Used |
|---|---|---|
| Q1 | Who is the most senior employee? | ORDER BY, LIMIT |
| Q2 | Which countries generate the most invoices? | GROUP BY, COUNT, ORDER BY |
| Q3 | What are the top 3 highest invoice values? | ORDER BY DESC, LIMIT |
| Q4 | Which city should we host the music festival in? | SUM, GROUP BY, ORDER BY |
| Q5 | Who is our single best (highest-spending) customer? | JOIN, SUM, GROUP BY |
Sample Query - Best City for a Music Festival:
SELECT billing_city, SUM(total) AS InvoiceTotal
FROM invoice
GROUP BY billing_city
ORDER BY InvoiceTotal DESC
LIMIT 1;| # | Business Question | SQL Concepts Used |
|---|---|---|
| Q1 | Who are all the Rock music listeners? (email list) | Multi-table JOIN, DISTINCT, Subquery |
| Q2 | Which are the top 10 rock bands by track count? | 4-table JOIN, COUNT, GROUP BY |
| Q3 | Which tracks are longer than the average song length? | Scalar Subquery, AVG, WHERE |
Sample Query - Top 10 Rock Artists:
SELECT artist.name, COUNT(artist.artist_id) AS number_of_songs
FROM track
JOIN album ON album.album_id = track.album_id
JOIN artist ON artist.artist_id = album.artist_id
JOIN genre ON genre.genre_id = track.genre_id
WHERE genre.name LIKE 'Rock'
GROUP BY artist.artist_id
ORDER BY number_of_songs DESC
LIMIT 10;| # | Business Question | SQL Concepts Used |
|---|---|---|
| Q1 | How much did each customer spend on the top-selling artist? | WITH CTE, 6-table JOIN, SUM, aggregation |
| Q2 | What is the most popular genre in each country? | WITH CTE + ROW_NUMBER() window function; also solved with Recursive CTE |
| Q3 | Who is the top-spending customer in each country? | WITH CTE + ROW_NUMBER() window function; also solved with Recursive CTE |
Sample Query - Most Popular Genre Per Country (CTE + Window Function):
WITH popular_genre AS (
SELECT
COUNT(invoice_line.quantity) AS purchases,
customer.country,
genre.name,
genre.genre_id,
ROW_NUMBER() OVER(
PARTITION BY customer.country
ORDER BY COUNT(invoice_line.quantity) DESC
) AS RowNo
FROM invoice_line
JOIN invoice ON invoice.invoice_id = invoice_line.invoice_id
JOIN customer ON customer.customer_id = invoice.customer_id
JOIN track ON track.track_id = invoice_line.track_id
JOIN genre ON genre.genre_id = track.genre_id
GROUP BY customer.country, genre.name, genre.genre_id
)
SELECT * FROM popular_genre WHERE RowNo <= 1;| Technique | Used In |
|---|---|
GROUP BY + HAVING |
Sales aggregation, customer ranking |
Multi-table JOIN (up to 6 tables) |
Customerβinvoiceβtrackβartist chains |
| Correlated & scalar subqueries | Track length filtering, top-N selection |
| Common Table Expressions (CTEs) | Advanced Q1, Q2, Q3 |
Window functions - ROW_NUMBER() OVER (PARTITION BY ...) |
Top genre/customer per country |
| Recursive CTEs | Alternative solutions for Q2 & Q3 |
DISTINCT for deduplication |
Email list generation |
Aggregate functions - SUM, COUNT, AVG, MAX |
Throughout all levels |
SQL-Music-Store-Analysis/
β
βββ ποΈ Music_Store_database.sql β Full database: schema + seed data
βββ π Music_Store_Query.sql β All 11 analytical queries (3 difficulty levels)
βββ πΌοΈ MusicDatabaseSchema.png β Visual ER diagram of all 11 tables
βββ π README.md β You are here
- PostgreSQL 13+ (recommended) - or any SQL-compatible RDBMS
git clone https://github.com/AmanManiTiwari/SQL-Music-Store-Analysis.git
cd SQL-Music-Store-Analysis-- In psql terminal
CREATE DATABASE music_store;
\c music_storepsql -U postgres -d music_store -f Music_Store_database.sqlpsql -U postgres -d music_store -f Music_Store_Query.sqlOr open Music_Store_Query.sql in pgAdmin, DBeaver, or any SQL IDE and run queries section by section.
ποΈ Best city for a music festival - identified by aggregating total invoice revenue per billing city
π Best customer - found by joining
customerandinvoicetables and ranking by total spend
πΈ Top 10 rock bands - ranked by track catalogue size via a 4-table join chain
π Most popular genre per country - solved two ways: using
ROW_NUMBER()windowing and a recursive CTE, demonstrating SQL flexibility
π° Top spender per country - window function partitioned by country delivers country-level customer leaderboards in a single query
| Tool | Purpose |
|---|---|
| PostgreSQL | Primary database engine |
| pgAdmin / DBeaver | Query execution & result visualization |
| SQL | All analysis - no Python or BI tools |
The skills exercised here directly map to day-to-day analyst work at companies like:
- Spotify / Apple Music - genre popularity by region, artist performance analytics
- Retail & e-commerce - top customer identification, city-level revenue analysis
- Any data team - writing clean, performant SQL that answers stakeholder questions without ambiguity
Aman Mani Tiwari - Data analyst building a portfolio that spans SQL, Excel dashboards, and machine learning.
If this project was useful, a β helps others find it - thank you!
