Dataford
Interview Guides
Upgrade
All questions/SQL & Data Manipulation/Compare Weekly User Activity Trends

Compare Weekly User Activity Trends

Medium
SQL & Data Manipulation
Asked at 12 companies12Window FunctionsLag/LeadDate Functions
Also asked at
GarminFractalSignifydSnapAI

Problem

Task

StreamWave wants to measure how each user’s activity changes from one week to the next. Write a SQL query to compare weekly behavior using window functions.

Requirements

  1. For each active user and calendar week, calculate:
    • total sessions
    • total minutes watched
  2. Compare each week to the user’s previous active week.
  3. Return the week-over-week change in both sessions and minutes watched.
  4. Only include rows where a previous week exists.
  5. Order the final output by user_id and week_start.

Table Definitions

users

columntypedescription
user_idINTPrimary key for each user
user_nameVARCHAR(100)User name
signup_dateDATEDate the user signed up
plan_typeVARCHAR(20)Subscription plan

watch_sessions

columntypedescription
session_idINTPrimary key for each session
user_idINTUser who created the session
session_startTIMESTAMPSession start time
minutes_watchedINTMinutes watched in the session
device_typeVARCHAR(20)Device used for the session

Sample Data

users

user_iduser_namesignup_dateplan_type
101Ava Patel2024-01-02Premium
102Ben Carter2024-01-05Basic
103Chloe Kim2024-01-10Premium

watch_sessions

session_iduser_idsession_startminutes_watcheddevice_type
10071012024-01-15 19:30:0020Mobile
10011012024-01-02 09:00:0030TV
10021012024-01-03 20:00:0045Mobile
10041022024-01-04 18:00:0025Web
10051022024-01-10 21:00:0050TV
10091032024-01-17 12:00:00NULLMobile

Expected Output

user_iduser_nameweek_starttotal_sessionstotal_minutes_watchedprevious_week_sessionsprevious_week_minutes_watchedsessions_wow_changeminutes_wow_change
101Ava Patel2024-01-1528027505
102Ben Carter2024-01-08250225025
103Chloe Kim2024-01-152352400-5

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)Full name of the user
signup_dateDATEDate the user signed up
plan_typeVARCHAR(20)Subscription plan type
watch_sessions
ColumnTypeDescription
session_idPKINTUnique session identifier
user_idINTUser associated with the session
session_startTIMESTAMPTimestamp when the session started
minutes_watchedINTMinutes watched during the session
device_typeVARCHAR(20)Device used for the session

Sample Data

Example 1
Input{"users":[["101","Ava Patel","2024-01-02","Premium"],["102","Ben Carter","2024-01-05","Basic"],["103","Chloe Kim","2024-01-10","Premium"],["104","Diego Lopez","2024-01-12","Basic"],["105","Emma Reed","2024-01-15",null],["106","Farah Ali","2024-01-18","Premium"],["107","Gavin Brooks","2024-01-20","Basic"],["108","Hana Suzuki","2024-01-22","Premium"],["109","Zane Cooper","2024-01-22","Annual"],["110","Zane Cooper","2024-01-22","Promo"],["111","Farah Ali","2024-01-03","Student"],["112","Willa ScottOutput[["101","Ava Patel","2024-01-15","2","80","2","75","0","5"],["102","Ben Carter","2024-01-01","4","-4","1","50","3","-54"],["102","Ben Carter","2024-01-08","4","80","4","-4","0","84"],["102","Ben Carter","2024-01-15","3","45","4","80","-1","-35"],["102","Ben Carter","2024-01-22","1","-10","3","45","-2","-55"],["103","Chloe Kim","2024-01-01","1","3","1","63","0","-60"],["103","Chloe Kim","2024-01-08","3","102","1","3","2","99"],["103","Chloe Kim","2024-01-15","2","35","3","102","-1","-67"],["103",

Problem

Task

StreamWave wants to measure how each user’s activity changes from one week to the next. Write a SQL query to compare weekly behavior using window functions.

Requirements

  1. For each active user and calendar week, calculate:
    • total sessions
    • total minutes watched
  2. Compare each week to the user’s previous active week.
  3. Return the week-over-week change in both sessions and minutes watched.
  4. Only include rows where a previous week exists.
  5. Order the final output by user_id and week_start.

Table Definitions

users

columntypedescription
user_idINTPrimary key for each user
user_nameVARCHAR(100)User name
signup_dateDATEDate the user signed up
plan_typeVARCHAR(20)Subscription plan

watch_sessions

columntypedescription
session_idINTPrimary key for each session
user_idINTUser who created the session
session_startTIMESTAMPSession start time
minutes_watchedINTMinutes watched in the session
device_typeVARCHAR(20)Device used for the session

Sample Data

users

user_iduser_namesignup_dateplan_type
101Ava Patel2024-01-02Premium
102Ben Carter2024-01-05Basic
103Chloe Kim2024-01-10Premium

watch_sessions

session_iduser_idsession_startminutes_watcheddevice_type
10071012024-01-15 19:30:0020Mobile
10011012024-01-02 09:00:0030TV
10021012024-01-03 20:00:0045Mobile
10041022024-01-04 18:00:0025Web
10051022024-01-10 21:00:0050TV
10091032024-01-17 12:00:00NULLMobile

Expected Output

user_iduser_nameweek_starttotal_sessionstotal_minutes_watchedprevious_week_sessionsprevious_week_minutes_watchedsessions_wow_changeminutes_wow_change
101Ava Patel2024-01-1528027505
102Ben Carter2024-01-08250225025
103Chloe Kim2024-01-152352400-5

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)Full name of the user
signup_dateDATEDate the user signed up
plan_typeVARCHAR(20)Subscription plan type
watch_sessions
ColumnTypeDescription
session_idPKINTUnique session identifier
user_idINTUser associated with the session
session_startTIMESTAMPTimestamp when the session started
minutes_watchedINTMinutes watched during the session
device_typeVARCHAR(20)Device used for the session

Sample Data

Example 1
Input{"users":[["101","Ava Patel","2024-01-02","Premium"],["102","Ben Carter","2024-01-05","Basic"],["103","Chloe Kim","2024-01-10","Premium"],["104","Diego Lopez","2024-01-12","Basic"],["105","Emma Reed","2024-01-15",null],["106","Farah Ali","2024-01-18","Premium"],["107","Gavin Brooks","2024-01-20","Basic"],["108","Hana Suzuki","2024-01-22","Premium"],["109","Zane Cooper","2024-01-22","Annual"],["110","Zane Cooper","2024-01-22","Promo"],["111","Farah Ali","2024-01-03","Student"],["112","Willa ScottOutput[["101","Ava Patel","2024-01-15","2","80","2","75","0","5"],["102","Ben Carter","2024-01-01","4","-4","1","50","3","-54"],["102","Ben Carter","2024-01-08","4","80","4","-4","0","84"],["102","Ben Carter","2024-01-15","3","45","4","80","-1","-35"],["102","Ben Carter","2024-01-22","1","-10","3","45","-2","-55"],["103","Chloe Kim","2024-01-01","1","3","1","63","0","-60"],["103","Chloe Kim","2024-01-08","3","102","1","3","2","99"],["103","Chloe Kim","2024-01-15","2","35","3","102","-1","-67"],["103",
Practice SQL
PostgreSQL
Open on desktop for the full SQL editor with syntax highlighting and autocomplete.
Up next
Meta7-Day Rolling Active UsersMediumPatientPointMonthly User Engagement Trend AnalysisMediumPublicis GroupeWeekly Active Purchaser Growth RateMedium
Next question