SQL QuestSQL Interview Questions › Conditional Logic

Referred vs Organic: First-Month Spend

MediumFreeQuerying BasicsConditional LogicNULL HandlingJoinsSubqueries & CTEs

Do referred users behave differently? A question that lives on NULL handling. users.referred_by holds the referrer's user_id, or NULL for organic signups. For every user, sum the amount_gbp of completed transactions within 30 days of signup — and give users with none a spend of 0, not NULL, so they count in the average.

Label each user source as referred or organic. Return source, users, avg_first_month_gbp (rounded to 2 decimals) and never_transacted (users whose first-month spend is 0). Order by avg_first_month_gbp descending.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

users

user_idsignup_datecountryhome_currencyplanplan_sincekyc_verified_atreferred_bybirth_year
12025-06-01IEEURstandard2025-06-012025-06-03 16:00:00NULL1972
22025-06-02FREURstandard2025-06-022025-06-04 16:00:00NULL1979
32025-06-03GBGBPstandard2025-06-032025-06-08 04:00:00NULL1977

transactions

txn_iduser_idtstypeamountcurrencyamount_gbpfee_gbpmerchant_categorymerchant_countrycounterparty_user_idstatusdecline_reason
112025-06-14 11:01:51fx_exchange32271.15JPY167.811.68NULLNULLNULLcompletedNULL
212025-06-14 11:17:45bill_payment59.53EUR50.60utilitiesIENULLcompletedNULL
372025-06-16 10:06:38card_payment122.27EUR103.930shoppingESNULLcompletedNULL

Expected output: source=referred, users=41, avg_first_month_gbp=287.04, never_transacted=11; source=organic, ...

Hint

In a CTE, LEFT JOIN transactions and put the completed-within-30-days test inside a CASE in the SUM; COALESCE(SUM(...), 0) turns a no-transaction user into 0. CASE WHEN referred_by IS NULL THEN 'organic' ELSE 'referred' END is the label.

Concepts

SELECT CASE COALESCE LEFT JOIN CTE julianday CASE + COALESCE

Practise the topic: SQL practice questions · CASE WHEN practice · NULL handling practice · JOIN practice · CTE practice

In these company practice sets

Revolut

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Movie Rating Tier BreakdownMedium · FreeGenre Financial ReportMedium · FreeDepartment Compensation ReportMedium · FreePivot: Order Status by CountryMedium · FreeGenre Box Office ReportMedium · FreeFare Imputation AnalysisMedium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test