You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Which Employee has the Highest total number of Customers?
Solution:
CONCATENATE the last name and first name of the Employees.
COUNT the customer ID to get the total number of customers.
SELECTe.LastName||''||e.FirstNameAS Employee, COUNT(c.customerid) AS Total_Customer
FROM Employee AS e
INNER JOIN Customer AS c
ONe.EmployeeId=c.SupportRepIdGROUP BY1ORDER BY1DESC;
Output
Employee
Total_Customer
Peacock Jane
21
Park Margaret
20
Johnson Steve
18
Insight:
There are three Sales support Reps managing the customers with Peacock Jane having the highest customers of 21 and Johnson Steve with 18 total customers.
2. Who are our top Customers according to Invoices?
Solution:
Select the first and last names of the customers and calculate the total Sum of their invoices.
SELECTC.FirstName||''||C.LastNameAS Customer_Name,
SUM(I.Total) AS Total_spent
FROM Invoice AS I
INNER JOIN Customer AS C
ONC.CustomerId=I.CustomerIdGROUP BY1ORDER BY2DESCLIMIT5;
Output
Customer_Name
Total_spent
Helena Holý
49.62
Richard Cunningham
47.62
Luis Rojas
46.62
Ladislav Kovács
45.62
Hugh O'Reilly
45.62
Insight:
Helena Holy, Richard Cunningham, Luis Rojas, Ladislav Kovacs, and Hugh O’Reilly are the top five customers who have spent the highest amount of money according to the invoice.
3. Who are the Rock Music Listeners? We want to know all Rock Music listeners' email, first names, last names, and Genres
Solution:
Select the email, first and last names of the customers, and the Genre and filter the Genre to Rock music.
SELECTC.Email, C.FirstName, C.LastName, G.NameAS Genre
FROM Customer C
INNER JOIN Invoice AS I
ONI.CustomerId=C.CustomerIdINNER JOIN InvoiceLine AS Il
ONIl.InvoiceId=I.InvoiceIdINNER JOIN Track AS T
ONT.TrackId=Il.TrackIdINNER JOIN Genre AS G
ONG.GenreId=T.GenreIdWHERE Genre ='Rock'GROUP BY1,2,3,4ORDER BY1;
We found out that all the 59 customers in the database have listened to Rock Music.
4. Who is writing the rock music?
Solution:
Select the Artist's name and count the number of rock music they have written.
SELECTAr.NameAs Artist, COUNT(G.name) AS Total_rock
FROM Artist AS Ar
INNER JOIN Album AS Al
ONAl.ArtistId=Ar.ArtistIdINNER JOIN Track AS T
ONT.AlbumId=Al.AlbumIdINNER JOIN Genre AS G
ONG.GenreId=T.GenreIdWHEREG.Name='Rock'GROUP BY1ORDER BY2DESCLIMIT10;
Output:
Artist
Total_rock
Led Zeppelin
114
U2
112
Deep Purple
92
Iron Maiden
81
Pearl Jam
54
Van Halen
52
Queen
45
The Rolling Stones
41
Creedence Clearwater Revival
40
Kiss
35
Insights:
Led Zeppelin tops the list of Artists who have written the most Rock Music with 114 songs followed Closely by U2 with 112 music.
5. Which artist has earned the most according to the Invoice Lines? Use this artist to find which customer spent the most on this artist.
a. Artist that has earned the most.
Solution:
Select the Artist’s name and
calculate the Total earned by multiplying the unit price by the quantity.
SELECTAr.NameAs Artist,
ROUND(SUM(T.UnitPrice*Il.Quantity),2) AS Total_earned
FROM Artist AS Ar
JOIN Album AS Al
ONAl.ArtistId=Ar.ArtistIdINNER JOIN Track AS T
ONT.AlbumId=Al.AlbumIdINNER JOIN InvoiceLine AS Il
ONIl.TrackId=T.TrackIdINNER JOIN Invoice AS I
ONI.InvoiceId=Il.InvoiceIdGROUP BY1ORDER BY2DESCLIMIT5;
Output:
Artist
Total_earned
Iron Maiden
138.6
U2
105.93
Metallica
90.09
Led Zeppelin
86.13
Lost
81.59
Insight:
The Artist who has earned the most according to the invoice lines is Iron Maiden with a total of $138.6
b. Customer who spent most on Iron Maiden
Use CTE to get the Artist name, Customer ID, Customer’s name, and the Amount spent by multiplying the unit price by the quantity.
From the CTE, select the Artist, Customer ID, Customer name, and the Amount Spent
Insert the query written in part as a Subquery in the WHERE clause to filter the result to only the artist that has earned the most.
WITH Customer_spending AS(
SELECTAr.NameAs Artist,
C.CustomerIdAS Customer_Id,
C.FirstNameAS First_Name,
C.LastNameAS Last_Name,
T.UnitPrice*Il.QuantityAS Amount_spent
FROM Artist AS Ar
INNER JOIN Album AS Al
ONAl.ArtistId=Ar.ArtistIdINNER JOIN Track AS T
ONT.AlbumId=Al.AlbumIdINNER JOIN InvoiceLine AS Il
INNER ONIl.TrackId=T.TrackIdINNER JOIN Invoice AS I
ONI.InvoiceId=Il.InvoiceIdINNER JOIN Customer AS C
ONC.CustomerId=I.CustomerIdORDER BY5DESC)
SELECT Artist,
Customer_Id,
First_Name,
Last_Name,
SUM(Amount_spent) AS Amount_spent
FROM Customer_spending
WHERE Artist = (SELECT Artist
FROM(SELECTAr.NameAs Artist,
ROUND(SUM(T.UnitPrice*Il.Quantity),2) AS Total_earned
FROM Artist AS Ar
INNER JOIN Album AS Al
ONAl.ArtistId=Ar.ArtistIdINNER JOIN Track AS T
ONT.AlbumId=Al.AlbumIdINNER JOIN InvoiceLine AS Il
ONIl.TrackId=T.TrackIdINNER JOIN Invoice AS I
ONI.InvoiceId=Il.InvoiceIdGROUP BY1ORDER BY2DESCLIMIT1) t1)
GROUP BY1,2,3,4ORDER BY5DESCLIMIT6;
Output:
Artist
Customer_Id
First_Name
Last_Name
Amount_spent
Iron Maiden
55
Mark
Taylor
17.82
Iron Maiden
35
Madalena
Sampaio
15.84
Iron Maiden
16
Frank
Harris
13.86
Iron Maiden
36
Hannah
Schneider
13.86
Iron Maiden
5
František
Wichterlová
8.91
Iron Maiden
27
Patrick
Gray
8.91
Insight:
Mark Taylor is the customer with the highest spending on our highest-earning Artist. He spent a total of $17.82.
6. List the Tracks that have a song length greater than the Average song length.
Solution:
Select the name and the Milliseconds.
Use a subquery to find the Average Song length.
Insert the subquery in the WHERE clause to filter the result.
SELECT Name,
Milliseconds AS Song_length_ms
FROM Track
WHERE Milliseconds > (SELECT ROUND(AVG(Milliseconds),2) AS Avg_Song_length FROM Track)
Output:
Name
Song_length_ms
Occupation / Precipice
5286953
Through a Looking Glass
5088838
Greetings from Earth, Pt. 1
2960293
The Man With Nine Lives
2956998
Battlestar Galactica, Pt. 2
2956081
Battlestar Galactica, Pt. 1
2952702
Murder On the Rising Star
2935894
Battlestar Galactica, Pt. 3
2927802
Take the Celestra
2927677
Fire In Space
2926593
7. Find out the most popular genre for each Country.
Solution:
Use a Subquery to get the Country, Genre ID, Genre name, and the number of purchases.
Write another subquery to select the Maximum Purchase from the first subquery.
Use the first subquery as query 3 to join the second subquery.
Select the Country, Genre ID, Genre name, and the maximum purchase from query 2 and query 3.
SELECTsub2.Country,
sub2.Purchases,
sub3.Genre_Id,
sub3.Genre_NameFROM (SELECT Country, MAX(purchases) as Purchases
FROM (SELECTC.CountryAS Country,
G.GenreIdAS Genre_Id,
G.NameAS Genre_Name,
COUNT(*) AS purchases
FROM Customer C
JOIN Invoice I
ONC.CustomerId=I.CustomerIdJOIN InvoiceLine Il
ONI.InvoiceId=Il.InvoiceIdJOIN Track T
ONIl.TrackId=T.TrackIdJOIN Genre G
ONG.GenreId=T.GenreIdGROUP BY1,2,3ORDER BY1) sub1 -- first subqueryGROUP BY1ORDER BY2) sub2 --- second subqueryJOIN (SELECTC.CountryAS Country,
G.GenreIdAS Genre_Id,
G.NameAS Genre_Name,
COUNT(*) AS purchases
FROM Customer C
JOIN Invoice I
ONC.CustomerId=I.CustomerIdJOIN InvoiceLine Il
ONI.InvoiceId=Il.InvoiceIdJOIN Track T
ONIl.TrackId=T.TrackIdJOIN Genre G
ONG.GenreId=T.GenreIdGROUP BY1,2,3ORDER BY1) sub3 --- third queryWHEREsub2.Country=sub3.CountryANDsub2.Purchases=sub3.purchasesORDER BY1,4;
Output:
There are 24 countries, below is the result for the first ten rows
Country
Purchases
Genre_Id
Genre_Name
Argentina
9
4
Alternative & Punk
Argentina
9
1
Rock
Australia
22
1
Rock
Austria
15
1
Rock
Belgium
21
1
Rock
Brazil
81
1
Rock
Canada
107
1
Rock
Chile
9
1
Rock
Czech Republic
25
1
Rock
Denmark
21
1
Rock
Insight:
Majority of the countries have Rock music as the most Listened music.