PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Data Manipulation (SQL/Python)/Block (Square)

Write left-join queries with tricky filters

Last updated: Mar 29, 2026

Quick Overview

This question evaluates SQL data-manipulation skills including LEFT JOIN semantics, aggregation functions like COUNT(DISTINCT) and SUM, GROUP BY/HAVING logic, and date-window filtering for referral and order analytics.

  • Medium
  • Block (Square)
  • Data Manipulation (SQL/Python)
  • Data Scientist

Write left-join queries with tricky filters

Company: Block (Square)

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: Medium

Interview Round: Technical Screen

Assume 'today' = '2025-09-01'. Do NOT use window functions (e.g., RANK). Use LEFT JOIN, COUNT(DISTINCT), GROUP BY, HAVING as appropriate. Schema: - users(user_id INT PRIMARY KEY, name TEXT, signup_dt DATE, country TEXT) - referrals(referrer_user_id INT, referred_user_id INT, referral_dt DATE) - orders(order_id INT PRIMARY KEY, user_id INT, order_dt DATE, amount DECIMAL(10,2)) Small ASCII samples: users +---------+-------+------------+---------+ | user_id | name | signup_dt | country | +---------+-------+------------+---------+ | 1 | Alice | 2025-08-20 | US | | 2 | Bob | 2025-08-22 | CA | | 3 | Chen | 2025-08-25 | CN | | 4 | Diana | 2025-08-29 | US | | 5 | Eva | 2025-08-31 | GB | +---------+-------+------------+---------+ referrals +------------------+------------------+------------+ | referrer_user_id | referred_user_id | referral_dt| +------------------+------------------+------------+ | 1 | 2 | 2025-08-22 | | 1 | 3 | 2025-08-25 | | 2 | 4 | 2025-08-29 | | 99 | 5 | 2025-08-31 | +------------------+------------------+------------+ orders +----------+---------+------------+--------+ | order_id | user_id | order_dt | amount | +----------+---------+------------+--------+ | 101 | 2 | 2025-08-30 | 50.00 | | 102 | 2 | 2025-09-01 | 25.00 | | 103 | 3 | 2025-09-01 | 10.00 | | 104 | 4 | 2025-08-29 | 20.00 | +----------+---------+------------+--------+ Answer the following (write exact SQL for each): A) For each referrer_user_id in referrals, return: referred_count (COUNT(DISTINCT referred_user_id)), buyers_last_7d (COUNT DISTINCT of referred users who placed an order between '2025-08-26' and '2025-09-01' inclusive), and revenue_last_7d (SUM of order amounts in that window). Include referrers with zero buyers/revenue. Group only by referrer_user_id. B) List all users who were referred (i.e., appear as referred_user_id) but have zero orders on or before '2025-09-01'. Output: referred_user_id, referrer_user_id. Use a LEFT JOIN to orders and HAVING to enforce zero. C) For each country, among users who were referred, compute: referred_users, buyers (users with at least one order on or before '2025-09-01'), and conversion_rate = buyers*1.0/referred_users rounded to 2 decimals. Include countries with buyers=0 but exclude countries with referred_users=0. D) The business asks for metrics filtered to country='UK'. Without assuming the code list, first show the exact query you would run to surface valid values (so you avoid returning 0 due to a non-existent filter), then provide the corrected metrics query using the appropriate value from the data (hint: sample data uses 'GB', not 'UK'). Briefly explain in a SQL comment why a naive WHERE country='UK' can silently return 0. E) Given an interview setting where the interviewer may be the only person allowed to execute queries, write the first two exploratory SELECTs you would ask them to run so you can understand table shape and safe join keys before attempting parts A–D.

Quick Answer: This question evaluates SQL data-manipulation skills including LEFT JOIN semantics, aggregation functions like COUNT(DISTINCT) and SUM, GROUP BY/HAVING logic, and date-window filtering for referral and order analytics.

Block (Square) logo
Block (Square)
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
2
0

Assume 'today' = '2025-09-01'. Do NOT use window functions (e.g., RANK). Use LEFT JOIN, COUNT(DISTINCT), GROUP BY, HAVING as appropriate.

Schema:

  • users(user_id INT PRIMARY KEY, name TEXT, signup_dt DATE, country TEXT)
  • referrals(referrer_user_id INT, referred_user_id INT, referral_dt DATE)
  • orders(order_id INT PRIMARY KEY, user_id INT, order_dt DATE, amount DECIMAL(10,2))

Small ASCII samples: users +---------+-------+------------+---------+ | user_id | name | signup_dt | country | +---------+-------+------------+---------+ | 1 | Alice | 2025-08-20 | US | | 2 | Bob | 2025-08-22 | CA | | 3 | Chen | 2025-08-25 | CN | | 4 | Diana | 2025-08-29 | US | | 5 | Eva | 2025-08-31 | GB | +---------+-------+------------+---------+

referrals +------------------+------------------+------------+ | referrer_user_id | referred_user_id | referral_dt| +------------------+------------------+------------+ | 1 | 2 | 2025-08-22 | | 1 | 3 | 2025-08-25 | | 2 | 4 | 2025-08-29 | | 99 | 5 | 2025-08-31 | +------------------+------------------+------------+

orders +----------+---------+------------+--------+ | order_id | user_id | order_dt | amount | +----------+---------+------------+--------+ | 101 | 2 | 2025-08-30 | 50.00 | | 102 | 2 | 2025-09-01 | 25.00 | | 103 | 3 | 2025-09-01 | 10.00 | | 104 | 4 | 2025-08-29 | 20.00 | +----------+---------+------------+--------+

Answer the following (write exact SQL for each): A) For each referrer_user_id in referrals, return: referred_count (COUNT(DISTINCT referred_user_id)), buyers_last_7d (COUNT DISTINCT of referred users who placed an order between '2025-08-26' and '2025-09-01' inclusive), and revenue_last_7d (SUM of order amounts in that window). Include referrers with zero buyers/revenue. Group only by referrer_user_id. B) List all users who were referred (i.e., appear as referred_user_id) but have zero orders on or before '2025-09-01'. Output: referred_user_id, referrer_user_id. Use a LEFT JOIN to orders and HAVING to enforce zero. C) For each country, among users who were referred, compute: referred_users, buyers (users with at least one order on or before '2025-09-01'), and conversion_rate = buyers*1.0/referred_users rounded to 2 decimals. Include countries with buyers=0 but exclude countries with referred_users=0. D) The business asks for metrics filtered to country='UK'. Without assuming the code list, first show the exact query you would run to surface valid values (so you avoid returning 0 due to a non-existent filter), then provide the corrected metrics query using the appropriate value from the data (hint: sample data uses 'GB', not 'UK'). Briefly explain in a SQL comment why a naive WHERE country='UK' can silently return 0. E) Given an interview setting where the interviewer may be the only person allowed to execute queries, write the first two exploratory SELECTs you would ask them to run so you can understand table shape and safe join keys before attempting parts A–D.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Data Manipulation (SQL/Python)•More Block (Square)•More Data Scientist•Block (Square) Data Scientist•Block (Square) Data Manipulation (SQL/Python)•Data Scientist Data Manipulation (SQL/Python)
PracHub

Master your tech interviews with 8,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.