PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Data Manipulation (SQL/Python)/Amazon

Write SQL window functions for D7 retention

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in SQL window functions, time-based aggregations, joins, null handling, and dense ranking for retention and revenue attribution within the Data Manipulation (SQL/Python) domain, emphasizing practical application of SQL for analytics.

  • medium
  • Amazon
  • Data Manipulation (SQL/Python)
  • Data Scientist

Write SQL window functions for D7 retention

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

Assume you have the following tables (timestamps are in UTC). **1) game_sessions** - user_id (STRING) - session_start_ts (TIMESTAMP) - country (STRING) Each row is one play session. **2) ad_impressions** - user_id (STRING) - impression_ts (TIMESTAMP) - ad_type (STRING) -- e.g., 'rewarded', 'interstitial' - revenue_usd (DECIMAL(10,4)) Each row is one ad impression. Write a SQL query (window functions encouraged) that outputs **one row per user** with: - user_id - first_play_date (DATE) = the user’s first session date - retained_d7 (INT) = 1 if the user had **any** session on (first_play_date + 7 days), else 0 - ad_revenue_first_7d (DECIMAL) = total ad revenue from impressions with impression_ts in [first_play_ts, first_play_ts + 7 days) - country - revenue_rank_in_country (INT) = rank users within each country by ad_revenue_first_7d (highest revenue rank = 1) Notes: - If a user has no ad impressions, ad_revenue_first_7d should be 0. - Define ties in ranking using dense rank.

Quick Answer: This question evaluates proficiency in SQL window functions, time-based aggregations, joins, null handling, and dense ranking for retention and revenue attribution within the Data Manipulation (SQL/Python) domain, emphasizing practical application of SQL for analytics.

Related Interview Questions

  • Find recommended friend pairs by shared songs - Amazon (medium)
  • Find recommended friend pairs by shared listening - Amazon (easy)
  • Find daily first-order merchants with SQL - Amazon (Medium)
  • Design student–course data models and SQL - Amazon (Medium)
  • Implement robust word counts and min/max - Amazon (Medium)
Amazon logo
Amazon
Nov 4, 2025, 12:00 AM
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
2
0

Assume you have the following tables (timestamps are in UTC).

1) game_sessions

  • user_id (STRING)
  • session_start_ts (TIMESTAMP)
  • country (STRING)

Each row is one play session.

2) ad_impressions

  • user_id (STRING)
  • impression_ts (TIMESTAMP)
  • ad_type (STRING) -- e.g., 'rewarded', 'interstitial'
  • revenue_usd (DECIMAL(10,4))

Each row is one ad impression.

Write a SQL query (window functions encouraged) that outputs one row per user with:

  • user_id
  • first_play_date (DATE) = the user’s first session date
  • retained_d7 (INT) = 1 if the user had any session on (first_play_date + 7 days), else 0
  • ad_revenue_first_7d (DECIMAL) = total ad revenue from impressions with impression_ts in [first_play_ts, first_play_ts + 7 days)
  • country
  • revenue_rank_in_country (INT) = rank users within each country by ad_revenue_first_7d (highest revenue rank = 1)

Notes:

  • If a user has no ad impressions, ad_revenue_first_7d should be 0.
  • Define ties in ranking using dense rank.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Data Manipulation (SQL/Python)•More Amazon•More Data Scientist•Amazon Data Scientist•Amazon Data Manipulation (SQL/Python)•Data Scientist Data Manipulation (SQL/Python)
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.