Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Rank Reels Creators by Country
00:00
5 left

Rank Reels Creators by Country

MediumSQL · PostgreSQL

Problem

Meta’s Instagram Reels team wants to identify the strongest creators by total watch time over the last 7 days. Write a SQL query to rank creators within each country and return the top 5 creators per country.

Requirements

  1. Use only watch events from the last 7 days based on watch_date.
  2. Sum watch_time_seconds for each creator within each country.
  3. Rank creators separately inside each country by total watch time, highest first.
  4. Return only the top 5 creators per country.
  5. Include the creator name, country, total watch time, and rank.
  6. If two creators tie on total watch time, break ties by smaller creator_id.

Output details

  • Treat DATE '2025-05-17' as today's date.
  • Use an inclusive seven-day range from DATE '2025-05-11' through DATE '2025-05-17'.
  • Return columns in this order: country, creator_id, creator_name, total_watch_time_seconds, country_rank.
  • Include creators with no qualifying watch events and assign them 0 total watch time.
  • Sort results by country ascending, country_rank ascending, and creator_id ascending, with NULL countries last.

Schema

reels_creators
ColumnTypeDescription
creator_idPKINTUnique Instagram Reels creator identifier
creator_nameVARCHAR(100)Creator display name
countryVARCHAR(50)Creator home country
account_statusVARCHAR(20)Creator account status such as active or paused
reels_watch_events
ColumnTypeDescription
event_idPKINTUnique watch event identifier
viewer_idINTViewer who watched the Reel
creator_idINTCreator whose Reel was watched
watch_dateDATEDate of the watch event
watch_time_secondsINTWatch time contributed by the event in seconds
surfaceVARCHAR(50)Meta surface where the watch happened
Tablesreels_creatorsreels_watch_events
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.