KatInterview

Referral funnel SQL: LEFT JOIN date windows and country filters

SQL & DataModel answer
MediumModel answerSQL & Datasqlleft-joingroup-by

Last updated

Write five SQL queries over users, referrals and orders that measure referral conversion and revenue without losing zero-activity rows, with no window functions.

You are analysing a referral program. Treat today as 2025-09-01. Window functions are not allowed. Use LEFT JOIN, COUNT(DISTINCT ...), GROUP BY and HAVING where they fit.

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))

Sample data

users

user_idnamesignup_dtcountry
1Alice2025-08-20US
2Bob2025-08-22CA
3Chen2025-08-25CN
4Diana2025-08-29US
5Eva2025-08-31GB

referrals

referrer_user_idreferred_user_idreferral_dt
122025-08-22
132025-08-25
242025-08-29
9952025-08-31

orders

order_iduser_idorder_dtamount
10122025-08-3050.00
10222025-09-0125.00
10332025-09-0110.00
10442025-08-2920.00

Tasks

Write the exact SQL for each part.

A) Referrer performance, last 7 days. For every referrer_user_id in referrals, return:

  • referred_count: distinct referred users.
  • buyers_last_7d: distinct referred users with an order between 2025-08-26 and 2025-09-01 inclusive.
  • revenue_last_7d: the sum of order amounts in that window.

Referrers with no buyers or revenue must still appear. Group only by referrer_user_id.

B) Referred users who never bought. List every referred user who has no orders on or before 2025-09-01. Output referred_user_id, referrer_user_id. Use a LEFT JOIN to orders and enforce the "zero" condition with HAVING.

C) Conversion by country. Among referred users only, return per country:

  • referred_users
  • buyers: users with at least one order on or before 2025-09-01.
  • conversion_rate: buyers * 1.0 / referred_users, rounded to 2 decimals.

Keep countries with zero buyers. Exclude countries with no referred users.

D) A filter that may match nothing. The business asks for the metrics from part C for country = 'UK'. Without assuming the code list:

  1. Write the query you would run first to discover which country values exist.
  2. Write the corrected metrics query using the value that actually appears in the data (the sample uses GB).
  3. Add a short SQL comment explaining why a naive WHERE country = 'UK' can silently return nothing or zero.

E) Read-only exploration. Suppose only a reviewer can execute queries on a shared screen. Write the first two exploratory SELECTs you would ask them to run to understand table shape and safe join keys before attempting A to D.

Hints

Hint 1

When a LEFT JOIN is filtered on the right-hand table, the filter must go in the ON clause. In WHERE it removes the NULL-padded rows and silently turns the join into an inner join.

Hint 2

COUNT(*) counts the NULL-padded row as 1, while COUNT(o.order_id) and COUNT(DISTINCT o.user_id) ignore NULLs. That is the key to the HAVING condition in B and the buyer counts in A and C.

Hint 3

Joining one referred user to several orders multiplies rows. COUNT(DISTINCT) protects the counts but not SUM, so dedupe the referral pairs first and wrap the sum in COALESCE.

Hint 4

A filter on the driving (preserved) table, such as users.country, is safe in WHERE. Before filtering on a categorical value, profile the column with GROUP BY plus LENGTH or TRIM to spot variants and whitespace.