Data exploration / SQL

SQL Portfolio Project

See how I answer two business questions using SQL queries in a dataset.

Role
Data Analyst
Status
Complete
Skills used
  • SQL
  • Joins
  • Aggregation
  • Filtering
  • Sorting

Business question 01

Which media format makes up most of the store's inventory and revenue?

Selecting columns

This selects the number of sales in each category, the total revenue for each media type, and each type’s share of all revenue as a percent rounded to 2 decimal places. A subquery calculates total revenue across all sales, which each category’s revenue is divided by to get its percent.

SQL
SELECT
    Track.UnitPrice,
    COUNT(*) AS 'Prodcut_Count',
    SUM(InvoiceLine.UnitPrice * InvoiceLine.Quantity) AS 'Value ($)',
    ROUND(SUM(InvoiceLine.UnitPrice * InvoiceLine.Quantity) * 100.0 / (SELECT SUM(UnitPrice * Quantity) FROM InvoiceLine), 2) AS 'Percent_Of_Revenue (%)',

Case

This is a simple if/else that labels each sale “Video” if its media type ID is 3 and “Audio” for any other ID. The labels go into a new column named Media_Category.

SQL
CASE
    WHEN MediaType.MediaTypeId = 3 THEN 'Video'
    ELSE 'Audio'
END AS Media_Category
FROM MediaType

Joining & Filtering

The joins connect MediaType to Track and Track to InvoiceLine through their shared ID keys, so each sale is connected to its media type. GROUP BY then combines all the sales into one row for Audio and one row for Video.

SQL
JOIN Track ON MediaType.MediaTypeId = Track.MediaTypeId
JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId
GROUP BY Media_Category
Business question 01 · output

The result

Audio makes up most of the store's inventory (2,129 products) and revenue (90.51%).

Query output for Business question 01
Media categoryPercent of revenueProduct countValueUnit price
Audio90.51%2,129$2,107.71$0.99
Video9.49%111$220.89$1.99
Raw output from DB Browser for SQLite
SQL query and two-row result showing Audio with 2,129 sales and 90.51 percent of revenue, and Video with 111 sales and 9.49 percent.
Business question 02

What are the top 5 highest grossing genres in the United States?

I compare revenue by genre to see which categories generate the most sales in the United States.

Selecting the relevant columns

I select the genre name and use the aggregate function SUM to total revenue (unit price × quantity) for each genre.

SQL
SELECT
    Genre.Name AS 'Genre',
    SUM(InvoiceLine.UnitPrice * InvoiceLine.Quantity) AS 'Revenue_USD'
FROM Track

Joining

I join the tables by matching each foreign key to the primary key it references, linking the sales data back to the genre.

SQL
JOIN Genre ON Track.GenreId = Genre.GenreId
JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId
JOIN Invoice ON InvoiceLine.InvoiceId = Invoice.InvoiceId

Filtering

WHERE filters to US invoices only. GROUP BY collapses the rows into one row per genre. ORDER BY sorts genres by revenue, highest first, and LIMIT keeps the top 5.

SQL
WHERE Invoice.BillingCountry = 'USA'
GROUP BY Genre
ORDER BY Revenue_USD DESC
LIMIT 5;
Business question 02 · output

The result

Rock is the highest grossing genre in the United States with $155.43 in revenue.

Query output for Business question 02
GenreRevenue
Rock$155.43
Latin$90.09
Metal$63.36
Alternative & Punk$49.50
TV Shows$27.86
Raw output from DB Browser for SQLite
DB Browser for SQLite showing the SQL query and its five-row results table for the highest grossing genres in the United States, led by Rock at $155.43.
See the process

Walkthrough

Walkthrough videoComing soon
The source

The database

Chinook is a digital media store database with 11 tables covering music, playlists, customers, employees, and sales.

11 tablesScroll to explore ↓

Artist

ColumnTypeKey
ArtistIdINTPK
NameNVARCHAR(120)—

Album

ColumnTypeKey
AlbumIdINTPK
TitleNVARCHAR(160)—
ArtistIdINTFK

Track

ColumnTypeKey
TrackIdINTPK
NameNVARCHAR(200)—
AlbumIdINTFK
MediaTypeIdINTFK
GenreIdINTFK
ComposerNVARCHAR(220)—
MillisecondsINT—
BytesINT—
UnitPriceNUMERIC(10,2)—

MediaType

ColumnTypeKey
MediaTypeIdINTPK
NameNVARCHAR(120)—

Genre

ColumnTypeKey
GenreIdINTPK
NameNVARCHAR(120)—

Playlist

ColumnTypeKey
PlaylistIdINTPK
NameNVARCHAR(120)—

PlaylistTrack

ColumnTypeKey
PlaylistIdINTPK, FK
TrackIdINTPK, FK

InvoiceLine

ColumnTypeKey
InvoiceLineIdINTPK
InvoiceIdINTFK
TrackIdINTFK
UnitPriceNUMERIC(10,2)—
QuantityINT—

Invoice

ColumnTypeKey
InvoiceIdINTPK
CustomerIdINTFK
InvoiceDateDATETIME—
BillingAddressNVARCHAR(70)—
BillingCityNVARCHAR(40)—
BillingStateNVARCHAR(40)—
BillingCountryNVARCHAR(40)—
BillingPostalCodeNVARCHAR(10)—
TotalNUMERIC(10,2)—

Customer

ColumnTypeKey
CustomerIdINTPK
FirstNameNVARCHAR(40)—
LastNameNVARCHAR(20)—
CompanyNVARCHAR(80)—
AddressNVARCHAR(70)—
CityNVARCHAR(40)—
StateNVARCHAR(40)—
CountryNVARCHAR(40)—
PostalCodeNVARCHAR(10)—
PhoneNVARCHAR(24)—
FaxNVARCHAR(24)—
EmailNVARCHAR(60)—
SupportRepIdINTFK

Employee

ColumnTypeKey
EmployeeIdINTPK
LastNameNVARCHAR(20)—
FirstNameNVARCHAR(20)—
TitleNVARCHAR(30)—
ReportsToINTFK
BirthDateDATETIME—
HireDateDATETIME—
AddressNVARCHAR(70)—
CityNVARCHAR(40)—
StateNVARCHAR(40)—
CountryNVARCHAR(40)—
PostalCodeNVARCHAR(10)—
PhoneNVARCHAR(24)—
FaxNVARCHAR(24)—
EmailNVARCHAR(60)—

lerocha / chinook-database (https://github.com/lerocha/chinook-database)

Next projectML Used Vehicle Regression ProjectBack to work