cswork / libraryOpen practice workspace

Coding questions · TikTok

Write SQL for TikTok Live creator metrics

This question evaluates SQL data-manipulation competencies such as joins, aggregations, group-by and window functions, handling missing activity rows, date-range filtering, country partitioning, and deterministic tie-breaking for top-k metrics.

You are analyzing TikTok Live sessions and their engagement.

Tables

live_room

Each row is a Live session (“room”) launched by a creator.

columntypedescription
dateDATECalendar date of the Live session (assume UTC).
creator_idBIGINTCreator who launched the Live.
room_idBIGINTUnique id of the Live room.
countrySTRINGCountry where the Live room is attributed (e.g., US, ID, UK).
live_typeSTRINGDevice type used to go live (e.g., mobile, pc).

Assume PK: (date, room_id).

live_room_activity

Each row contains aggregated engagement for a room on that date.

columntypedescription
dateDATESame date as the Live session.
room_idBIGINTLive room id.
watch_cntINTNumber of viewers who watched that room.
gift_recieved_cntINTNumber of gifts received in that room.

Assume PK: (date, room_id).

Join relationship

Join the tables on (date, room_id). If a room has no activity row, assume watch_cnt = 0 and gift_recieved_cnt = 0.


Questions (write SQL only)

Q1

For the period 2022-10-01 to 2022-10-07 (inclusive), in the US, find how many creators launched more than 3 mobile Live sessions.

Output:

  • creator_count (number of distinct creator_id meeting the condition)

Q2 (Medium)

In October 2022, find the top 5 creators by total gifts received for each country.

Notes:

  • Total gifts received for a creator in a country = sum of gift_recieved_cnt across all that creator’s rooms in that country during October 2022.
  • If ties occur at rank 5, break ties by creator_id ascending (or specify a deterministic tie-break).

Output (recommended):

  • country
  • creator_id
  • total_gifts_received
  • rank_in_country (1–5)

Q3 (Hard)

For yesterday’s date (relative to query run date), calculate the count of creators who satisfy both:

  1. The creator went live more than once that day (i.e., launched at least 2 rooms).
  2. Every Live room they launched that day had watch_cnt >= 500.

Output:

  • qualified_creator_count

US creators with more than 3 mobile Live sessions (2022-10-01 to 2022-10-07)

TikTok captures Live room launches in live_room.

For the time period from 2022-10-01 to 2022-10-07 (inclusive), in the US, find how many distinct creators launched more than 3 mobile Live sessions.

Return a single row with the count of such creators.

Table live_room

columntypeconstraints
dateDATENOT NULL
creator_idBIGINTNOT NULL
room_idINTNOT NULL
countryVARCHAR(2)NOT NULL
live_typeVARCHAR(10)NOT NULL

Sample data:

countrycreator_iddatelive_typeroom_id
US23159232022-10-01mobile3268
US23159232022-10-01pc3269
US23159232022-10-02mobile3274
US23159232022-10-03mobile3275
US23159232022-10-05mobile3276
US23285672022-10-03pc3271
US23285672022-10-03pc3272
US23285672022-10-04mobile3277
US23285672022-10-06mobile3278
US23285672022-10-07mobile3279
US24000002022-10-02mobile3283
US24111112022-10-12pc3285
ID23942262022-10-02mobile3270
UK23781232022-10-04mobile3273
UK23781232022-10-10pc3280
ID23942262022-10-12mobile3281
UK23800002022-10-15mobile3282
US24000002022-10-20mobile3284
ID23999992022-10-20mobile3286
US30012025-05-31mobile9001
US30012025-05-31pc9002
UK30022025-05-31mobile9003
UK30022025-05-31mobile9004
US30032025-05-31mobile9005

Table live_room_activity

columntypeconstraints
dateDATENOT NULL
room_idINTNOT NULL
watch_cntINTNOT NULL
gift_recieved_cntINTNOT NULL

Sample data:

dategift_recieved_cntroom_idwatch_cnt
2022-10-01500032682000
2022-10-0110003269500
2022-10-02150032741200
2022-10-032003275700
2022-10-058003276900
2022-10-02200032702500
2022-10-035003271900
2022-10-03703272300
2022-10-04100032731000
2022-10-0412003277650
2022-10-069003278800
2022-10-07503279550
2022-10-10300032801400
2022-10-1250032811100
2022-10-15400032822000
2022-10-02600032831800
2022-10-201003284400
2022-10-127003285900
2022-10-20250032861500
2025-05-311009001600
2025-05-31509002500
2025-05-31109003800
2025-05-31209004400
2025-05-31590051000

Expected output

One row with the number of US creators who launched more than 3 mobile Live sessions between 2022-10-01 and 2022-10-07 (inclusive).

creator_cnt
1

Hints

  1. Filter to the date range, country='US', and live_type='mobile'.
  2. Group by creator_id and keep creators with COUNT(*) > 3, then count them.

Top 5 creators by gifts received per country (October 2022)

TikTok captures Live room launches in live_room and per-room metrics in live_room_activity.

In October 2022 (2022-10-01 to 2022-10-31 inclusive), find the top 5 creators by total gifts received for each country.

Output columns:

  • country
  • creator_id
  • total_gift_received
  • country_rank (1 = highest total gifts for that country)

If a country has fewer than 5 creators, return all of them. Break ties by smaller creator_id first.

Table live_room

columntypeconstraints
dateDATENOT NULL
creator_idBIGINTNOT NULL
room_idINTNOT NULL
countryVARCHAR(2)NOT NULL
live_typeVARCHAR(10)NOT NULL

Sample data:

countrycreator_iddatelive_typeroom_id
US23159232022-10-01mobile3268
US23159232022-10-01pc3269
US23159232022-10-02mobile3274
US23159232022-10-03mobile3275
US23159232022-10-05mobile3276
US23285672022-10-03pc3271
US23285672022-10-03pc3272
US23285672022-10-04mobile3277
US23285672022-10-06mobile3278
US23285672022-10-07mobile3279
US24000002022-10-02mobile3283
US24000002022-10-20mobile3284
US24111112022-10-12pc3285
ID23942262022-10-02mobile3270
ID23942262022-10-12mobile3281
ID23999992022-10-20mobile3286
UK23781232022-10-04mobile3273
UK23781232022-10-10pc3280
UK23800002022-10-15mobile3282
US30012025-05-31mobile9001
US30012025-05-31pc9002
UK30022025-05-31mobile9003
UK30022025-05-31mobile9004
US30032025-05-31mobile9005

Table live_room_activity

columntypeconstraints
dateDATENOT NULL
room_idINTNOT NULL
watch_cntINTNOT NULL
gift_recieved_cntINTNOT NULL

Sample data:

dategift_recieved_cntroom_idwatch_cnt
2022-10-01500032682000
2022-10-0110003269500
2022-10-02150032741200
2022-10-032003275700
2022-10-058003276900
2022-10-02200032702500
2022-10-035003271900
2022-10-03703272300
2022-10-04100032731000
2022-10-0412003277650
2022-10-069003278800
2022-10-07503279550
2022-10-10300032801400
2022-10-1250032811100
2022-10-15400032822000
2022-10-02600032831800
2022-10-201003284400
2022-10-127003285900
2022-10-20250032861500
2025-05-311009001600
2025-05-31509002500
2025-05-31109003800
2025-05-31209004400
2025-05-31590051000

Expected output

Up to 5 rows per country for October 2022, ordered within each country by total_gift_received desc, then creator_id asc. Columns: country, creator_id, total_gift_received, country_rank.

countrycountry_rankcreator_idtotal_gift_received
ID123942262500
ID223999992500
UK123781234000
UK223800004000
US123159238500
US224000006100
US323285672720
US42411111700

Hints

  1. Join live_room to live_room_activity on (date, room_id) to attribute gifts to creators and countries.
  2. Aggregate gifts per (country, creator_id), then use a window function to rank within each country.

Creators who went live more than once with watch_cnt >= 500 in every room (2025-05-31)

Treat “yesterday” as 2025-05-31.

For 2025-05-31, calculate the number of creators who satisfy BOTH criteria:

  1. The creator went live more than once on that date.
  2. Every Live room they launched that date has watch_cnt >= 500.

Return a single row with the count of such creators.

Table live_room

columntypeconstraints
dateDATENOT NULL
creator_idBIGINTNOT NULL
room_idINTNOT NULL
countryVARCHAR(2)NOT NULL
live_typeVARCHAR(10)NOT NULL

Sample data:

countrycreator_iddatelive_typeroom_id
US23159232022-10-01mobile3268
US23159232022-10-01pc3269
ID23942262022-10-02mobile3270
US23285672022-10-03pc3271
US23285672022-10-03pc3272
UK23781232022-10-04mobile3273
US30012025-05-31mobile9001
US30012025-05-31pc9002
UK30022025-05-31mobile9003
UK30022025-05-31mobile9004
US30032025-05-31mobile9005

Table live_room_activity

columntypeconstraints
dateDATENOT NULL
room_idINTNOT NULL
watch_cntINTNOT NULL
gift_recieved_cntINTNOT NULL

Sample data:

dategift_recieved_cntroom_idwatch_cnt
2022-10-01500032682000
2022-10-0110003269500
2022-10-02200032702500
2022-10-035003271900
2022-10-03703272300
2022-10-04100032731000
2025-05-311009001600
2025-05-31509002500
2025-05-31109003800
2025-05-31209004400
2025-05-31590051000

Expected output

One row with the number of creators who went live more than once on 2025-05-31 and have watch_cnt >= 500 for every room they launched that day.

creators_cnt
1

Hints

  1. First restrict to 2025-05-31 and join activity to get watch_cnt per room.
  2. Use HAVING COUNT(*) > 1 and enforce the 'every room' condition via MIN(watch_cnt) >= 500 (or SUM(CASE WHEN watch_cnt < 500 THEN 1 END)=0).