Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL With Window Functions

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL With Window Functions. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

TikTok's creator analytics team wants to identify the strongest public videos for each active creator. Write a PostgreSQL query that calculates January engagement and ranks videos within each creator.

Requirements

  1. Include only public videos published in January 2025 by active creators.
  2. Calculate video engagement as likes plus comments plus shares from metrics recorded during January 2025. Treat NULL metrics and videos without metric rows as zero.
  3. Use a window function to return the top two videos per creator, breaking engagement ties with the smaller video_id.

Schema

creators
ColumnTypeDescription
creator_idPKINTUnique creator identifier
handleVARCHAR(50)TikTok creator handle
creator_tierVARCHAR(20)Creator classification
is_activeBOOLEANWhether the creator is active
videos
ColumnTypeDescription
video_idPKINTUnique video identifier
creator_idINTCreator who owns the video
captionVARCHAR(200)Video caption
published_atDATEDate the video was published
visibilityVARCHAR(20)Visibility status of the video
video_daily_metrics
ColumnTypeDescription
metric_idPKINTUnique metrics record identifier
video_idINTVideo associated with the metrics
metric_dateDATEDate of the daily metrics
likesINTNumber of likes
commentsINTNumber of comments
sharesINTNumber of shares
Tablescreatorsvideosvideo_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results