-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusic Store Data Analysis.sql
More file actions
120 lines (91 loc) · 4.21 KB
/
Copy pathMusic Store Data Analysis.sql
File metadata and controls
120 lines (91 loc) · 4.21 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
/* Question Set 1 - Easy */
/* Q1: Who is the Junior most employee based on job title? */
SELECT title, last_name, first_name
FROM employee
ORDER BY levels ASC
limit 1;
/* Q2: Sort the countries wrt number of Invoices in descending order? */
SELECT COUNT(*) AS No_of_invoices, billing_country
FROM invoice
GROUP BY billing_country
ORDER BY No_of_invoices DESC;
/* Q3: What are least 3 values of total invoice? */
SELECT distinct(total)
FROM invoice
ORDER BY total asc limit 3;
/* Q4: Which Country has the best customers? We would like to throw a promotional Music Festival in that country we made the most money.
Write a query that returns that one country has the highest sum of invoice totals.
Return both the country name & sum of all invoice totals */
SELECT billing_country,SUM(total) AS InvoiceTotal
FROM invoice
GROUP BY billing_country
ORDER BY InvoiceTotal DESC limit 1;
/* Q5: Who is the best customer? The customer who has spent the most money will be declared the best customer.
Write a query that returns the person who has spent the most money.*/
SELECT customer.customer_id, first_name, last_name,
SUM(total) AS total_spending FROM customer
JOIN invoice ON customer.customer_id = invoice.customer_id
GROUP BY customer_id, first_name, last_name
ORDER BY total_spending DESC limit 1;
/* Question Set 2 - Moderate */
/* Q1: Write query to return the email, first name, last name, & Genre of all Rock Music listeners.
Return your list ordered alphabetically by email starting with A. */
SELECT DISTINCT email,first_name, last_name
FROM customer
JOIN invoice ON customer.customer_id = invoice.customer_id
JOIN invoice_line ON invoice.invoice_id = invoice_line.invoice_id
WHERE track_id IN(
SELECT track_id FROM track
JOIN genre ON track.genre_id = genre.genre_id
WHERE genre.name LIKE 'Rock'
)
ORDER BY email;
/* Q2: Let's invite the artists who have written the most rock music in our dataset.
Write a query that returns the Artist name and total track count of the top 10 rock bands. */
SELECT artist.artist_id, artist.name as artist_name,
COUNT(artist.artist_id) AS number_of_songs
FROM track
JOIN album2 ON album2.album_id = track.album_id
JOIN artist ON artist.artist_id = album2.artist_id
JOIN genre ON genre.genre_id = track.genre_id
WHERE genre.name = 'Rock'
GROUP BY artist.artist_id, artist.name
ORDER BY number_of_songs DESC
LIMIT 10;
/* Q3: Return all the track names that have a song length longer than the average song length.
Return the Name and Milliseconds for each track. Order by the song length with the longest songs listed first. */
SELECT name, milliseconds
FROM track
WHERE milliseconds > (
SELECT AVG(milliseconds) AS avg_track_length
FROM track )
ORDER BY milliseconds DESC;
/* Question Set 3 - Advance */
/* Q1: Find how much amount spent by each customer on artists? Write a query to return customer name, artist name and total spent */
/* Steps to Solve: First, find which artist has earned the most according to the InvoiceLines. Now use this artist to find
which customer spent the most on this artist. For this query, you will need to use the Invoice, InvoiceLine, Track, Customer,
Album, and Artist tables. Note, this one is tricky because the Total spent in the Invoice table might not be on a single product,
so you need to use the InvoiceLine table to find out how many of each product was purchased, and then multiply this by the price
for each artist. */
-- CTE(Common Table Expression) => best_selling_artist()
WITH best_selling_artist AS (
SELECT artist.artist_id AS artist_id, artist.name AS artist_name,
SUM(invoice_line.unit_price * invoice_line.quantity) AS total_sales
FROM invoice_line
JOIN track ON track.track_id = invoice_line.track_id
JOIN album2 ON album2.album_id = track.album_id
JOIN artist ON artist.artist_id = album2.artist_id
GROUP BY 1,2
ORDER BY 3 DESC
LIMIT 1
)
SELECT c.customer_id, c.first_name, c.last_name, bsa.artist_name,
SUM(il.unit_price * il.quantity) AS amount_spent
FROM invoice i
JOIN customer c ON c.customer_id = i.customer_id
JOIN invoice_line il ON il.invoice_id = i.invoice_id
JOIN track t ON t.track_id = il.track_id
JOIN album2 alb ON alb.album_id = t.album_id
JOIN best_selling_artist bsa ON bsa.artist_id = alb.artist_id
GROUP BY 1,2,3,4
ORDER BY 5 DESC;