Practice Free
HardWindow Functions High Frequency

Card Transaction Velocity with Window COUNT()

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.

Solve in Workbench

Problem Statement

For each transaction in `card_transactions`, compute the cumulative count of transactions executed on that specific card up to and including the current transaction. Return: 1. `txn_id` 2. `card_token` 3. `merchant_name` 4. `amount_usd` 5. `card_txn_sequence`: `COUNT(*) OVER (PARTITION BY card_token ORDER BY txn_time ASC)` Order results by `card_token` ascending, then by `card_txn_sequence` ascending.

Why Technical Interviewers Ask This

Automated credit card fraud bots test stolen card numbers by making rapid micro-transactions in quick succession. Risk engineers use cumulative window counts to detect card usage frequency.

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 window functions constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        txn_id,
        card_token,
        merchant_name,
        amount_usd,
        COUNT(*) OVER (PARTITION BY card_token ORDER BY txn_time ASC) AS card_txn_sequence
      FROM card_transactions
      ORDER BY card_token ASC, card_txn_sequence ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Window Functions. It meets the target criteria: Transaction log showing the incremental card usage sequence number.

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 Window Functions 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.

More FAANG SQL Interview Questions

View all questions