by Tatiana Kurilo
September, 2018
library(DBI)
db <- dbConnect(RSQLite::SQLite(), dbname = "chinook.db")
--Highest Album Price
SELECT a.Title, g.Name AS Genre, SUM(t.UnitPrice) AS Price
FROM Track AS t
JOIN Album AS a ON a.AlbumId = t.AlbumId
JOIN Genre AS g ON t.GenreId = g.GenreID
GROUP BY a.AlbumId
ORDER BY Price DESC
LIMIT 1;
1 records
Title Genre Price
Greatest Hits Metal 56.43
--Average Album Price By Genre
SELECT Genre, AVG(Price) as Avg_Album_Price
FROM (SELECT a.Title, g.Name AS Genre, SUM(t.UnitPrice) AS Price
      FROM Track AS t
      JOIN Album AS a ON a.AlbumID = t.AlbumID
      JOIN Genre AS g ON t.GenreId = g.GenreID
      GROUP BY a.AlbumID
      ORDER BY Price DESC ) album_prices
GROUP BY 1
ORDER BY 2 DESC;
Displaying records 1 - 10
Genre Avg_Album_Price
Comedy 49.75000
Drama 43.78000
Sci Fi & Fantasy 42.78500
TV Shows 31.04400
Easy Listening 23.76000
Hip Hop/Rap 17.32500
Pop 15.84000
R&B/Soul 15.09750
Latin 15.05308
Bossa Nova 14.85000
--Sold Tracks By Genre
SELECT Genre, AVG(Sold)
FROM (SELECT t.Name, g.Name AS Genre, t.TrackId IN (SELECT il.TrackId FROM InvoiceLine as il) AS Sold
      FROM Track AS t
      JOIN Genre AS g ON t.GenreId = g.GenreId) sub
GROUP BY 1
ORDER BY 2 DESC;
Displaying records 1 - 10
Genre AVG(Sold)
Bossa Nova 0.9333333
Sci Fi & Fantasy 0.7692308
Blues 0.6543210
Metal 0.6176471
Alternative & Punk 0.6114458
R&B/Soul 0.6065574
Latin 0.5872193
Rock 0.5744025
Pop 0.5416667
Jazz 0.5230769
--Most Popular Genres By States In USA
SELECT BillingState AS state, Genre, MAX(num_tracks) AS NumberOfPurchasedTracks
FROM (SELECT i.BillingState, g.Name AS Genre, count(il.TrackID) AS num_tracks
      FROM Invoice AS i
      JOIN InvoiceLine AS il ON i.InvoiceId = il.InvoiceId AND i.BillingState <> "NA" AND i.BillingCountry = "USA"
      JOIN Track AS t ON il.TrackId = t.TrackId
      JOIN Genre AS g ON t.GenreId = g.GenreId
      GROUP BY 2
      ORDER BY 3 DESC) sub
GROUP BY 1;
Displaying records 1 - 10
state Genre NumberOfPurchasedTracks
AZ Alternative & Punk 50
CA Latin 91
FL TV Shows 14
IL Alternative 5
NV Reggae 6
NY Metal 64
TX Drama 6
UT Comedy 8
WA Easy Listening 3
WI Rock 157
--Unique Customers By Composer
SELECT t.Composer, COUNT(DISTINCT i.CustomerId)
FROM Track AS t
JOIN InvoiceLine AS il ON t.TrackId = il.TrackId AND t.Composer <> "NA"
JOIN Invoice AS i ON il.InvoiceId = i.InvoiceId
GROUP BY 1
ORDER BY 2 DESC;
Displaying records 1 - 10
Composer COUNT(DISTINCT i.CustomerId)
Steve Harris 23
U2 14
Herbert Vianna 11
Jimmy Page, Robert Plant 11
Billy Corgan 10
Bill Berry-Peter Buck-Mike Mills-Michael Stipe 9
Gilberto Gil 9
Chris Cornell 8
J. C. Fogerty 8
Robert Plant 8
--Number Of Composers With More Than 1 Listener
SELECT COUNT(num_listeners) AS more_than_one
FROM (SELECT t.Composer, count(DISTINCT i.CustomerId) AS num_listeners
      FROM Track AS t
      JOIN InvoiceLine AS il ON t.TrackId = il.TrackId AND t.Composer <> "NA"
      JOIN Invoice AS i ON il.InvoiceId = i.InvoiceId
      GROUP BY 1
      ORDER BY 2 DESC) sub
WHERE num_listeners > 1
1 records
more_than_one
254