Most-watched title in each country

window functions

Most-watched title in each country

Netflix SQL Interview Question

Netflix shows a Top 10 row that is different in every country. The content team wants to know which title was watched the most in each country.

For each country, find the title_name with the most total minutes watched, and that total as total_minutes. Each country has one clear top title, so you do not need to handle ties.

Asked of

  • Data Analyst
  • Product Analyst
  • Data Scientist
  • Analytics Engineer
  • Data Engineer

viewing_activityTable16 rows

Column NameType
view_idBIGINT
profile_idBIGINT
title_idBIGINT
countryVARCHAR
watch_dateDATE
minutes_watchedBIGINT

titlesTable6 rows

Column NameType
title_idBIGINT
title_nameVARCHAR
content_typeVARCHAR
genreVARCHAR
runtime_minutesBIGINT

viewing_activityExample Input

view_idprofile_idtitle_idcountrywatch_dateminutes_watched
110011United States2024-08-01118
210024United States2024-08-0158
310034United States2024-08-0230
410043United States2024-08-0245
510051Brazil2024-08-0260
610066Brazil2024-08-0352
710076Brazil2024-08-0352
810082Brazil2024-08-0392
910095India2024-08-04101
1010103India2024-08-0420

titlesExample Input

title_idtitle_namecontent_typegenreruntime_minutes
1Midnight HeistMovieThriller118
2Ocean DeepMovieDocumentary92
3Kitchen WarsSeriesReality45
4The Last ColonySeriesSci-Fi58
5Laugh TrackMovieComedy101
6Crown of AshSeriesDrama52

Example Output

countrytitle_nametotal_minutes
BrazilCrown of Ash104
IndiaLaugh Track101
United StatesMidnight Heist118

Explanation

In Brazil, Crown of Ash was watched twice for 52 minutes each, a total of 104 minutes. That beats Ocean Deep, even though its single 92-minute view was the longest in Brazil. The ranking is based on total minutes, not on individual views.

The example above is a small slice of the data. Your query runs against the full tables.