SQL QuestSQL Interview Questions › Joins

Cards That Never Spend at Home

HardProQuerying BasicsJoinsAggregation & GroupingConditional Logic

"Never" is a statement about a whole group, so it cannot live in a WHERE clause. WHERE decides one row at a time. WHERE m.country <> a.country answers a different question — "has at least one foreign transaction" — and in this ledger that is almost everybody: 1,715 of 2,165 transactions cross a border.

The cards worth looking at are the ones that have never transacted at a merchant in the cardholder's own country. Express it as a condition on the group: count the home-country transactions with a conditional SUM and require that count to be zero in HAVING.

Return account_id, email, country (the cardholder's), txn_count, merchant_countries (distinct merchant countries they used) and total_spend (rounded to 2 decimals). 24 accounts qualify. Order by txn_count descending, then account_id ascending.

The filter must be HAVING SUM(CASE WHEN m.country = a.country THEN 1 ELSE 0 END) = 0 — a count of the rows you do NOT want, required to be zero. Filtering those rows out with WHERE first would delete the evidence you are testing for and every account would pass.

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

accounts

account_idemailsignup_atcountrydevice_fingerprintip_blockstatus
1user1@example.com2026-02-24T00:00:00.000ZTRdev_13c0cdad41.116.196.153active
2user2@inbox.dev2025-05-13T00:00:00.000ZJPdev_26576d4938.98.201.102active
3user3@inbox.dev2026-04-25T00:00:00.000ZJPdev_42f1a115190.77.45.244flagged

transactions

txn_idaccount_idamounttxn_atmerchant_idlatlngstatus
1149151.842026-03-04T12:39:39.078Z2440.342-74.4092completed
22110.722026-03-04T13:04:50.641Z752.936613.3229completed
335158.782026-03-04T13:27:15.528Z2552.739713.3134completed

merchants

merchant_idnamecategorycountryrisk_tier
1BigBox MartGroceryTRhigh
2Quick StopElectronicsJPhigh
3Aurora CafeTravelTRhigh

Expected output: 188 user188@example.com JP 18 4 4457.79; 20 user20@inbox.dev JP 13 4 1357.35; ...

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

Concepts

SELECT JOIN GROUP BY HAVING CASE COUNT DISTINCT GROUP BY + HAVING

Practise the topic: SQL practice questions · JOIN practice · GROUP BY exercises · CASE WHEN practice · Advanced SQL interview questions

In these company practice sets

Capital One · Wise

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

Related questions

Multi-CTE Revenue PipelineHard · FreeEmployees Earning More Than ManagerHard · ProEmployees with Similar SalariesHard · ProMoving Average with Dynamic WindowHard · ProDetect Repeat Buyers Within 7 DaysHard · ProRecursive Team Size RollupHard · Pro

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