MediumAggregations
ATM Cash Depletion & Peak Withdrawal Volume
Frequently Asked By:StripeCoinbaseBloomberg
Interactive In-Browser Workspace
Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.
Problem Statement
Group transactions in `atm_withdrawals` by `terminal_id` and `branch_location`.
Calculate:
1. `terminal_id`
2. `branch_location`
3. `withdrawal_count`: Total count of withdrawal events
4. `total_cash_dispensed`: Sum of `dispensed_usd`
5. `avg_withdrawal_size`: `ROUND(AVG(dispensed_usd), 2)`
Use `HAVING` to only include terminals where `total_cash_dispensed >= 20000.00` AND `withdrawal_count >= 3`.
Order results by `total_cash_dispensed` descending.
Why Technical Interviewers Ask This
Bank vault logistics must route cash replenishment trucks before ATMs run dry over holiday weekends. The operations desk needs a list of ATM terminals where total cash dispensed today exceeds $20,000 across at least 3 transactions.
Common Pitfalls & Interview Traps
Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass aggregations constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLSELECT
terminal_id,
branch_location,
COUNT(*) AS withdrawal_count,
SUM(dispensed_usd) AS total_cash_dispensed,
ROUND(AVG(dispensed_usd), 2) AS avg_withdrawal_size
FROM atm_withdrawals
GROUP BY terminal_id, branch_location
HAVING SUM(dispensed_usd) >= 20000.00 AND COUNT(*) >= 3
ORDER BY total_cash_dispensed DESC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: High-volume ATMs requiring priority cash restocking, sorted by cash dispensed.Edge Cases to Verbally Clarify with Your Interviewer
The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.
Frequently Asked Follow-Up Questions
What makes this question a favorite among Stripe and Coinbase interviewers?
Interviewers use this problem to test whether candidates write defensive, scalable Aggregations queries that run efficiently on multi-million row production databases without excessive memory usage.
How does the query optimizer execute this query?
Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.