Prior Seven Day Transaction Velocity Features in SQL

Last updated: November 29, 2026

Quick Overview

From a transactions table (txn_id, user_id, ts, amount, status), compute for every transaction the number of the same user's transactions in the 7 days before it, their total amount, and how many were declined, using only strictly earlier transactions.

Chalk
Data Manipulation (SQL/Python)
Data Engineer
Chalk
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


From a transactions table (txn_id, user_id, ts, amount, status), compute for every transaction the number of the same user's transactions in the 7 days before it, their total amount, and how many were declined, using only strictly earlier transactions.

Velocity features are central to the fraud and underwriting use cases Chalk serves for fintech customers, and Chalk lets teams define features in SQL against warehouse sources. The interviewer checks window boundaries, exclusion of the current row, ties at equal timestamps, and whether the query would leak future data into training rows.

What the Interviewer Expects
  • Restricts to strictly earlier transactions so the current one never counts itself
  • Handles equal timestamps deliberately
  • Uses a LEFT JOIN or window frame so users with no history get zero, not missing rows
  • Counts declines with conditional aggregation
  • Discusses performance and indexing on (user_id, ts)
Key Topics to Cover
SQL
self join
window functions
velocity features
point-in-time correctness
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • Rewrite this with a window function frame. What breaks when two transactions share a timestamp?
  • How would you compute the same feature incrementally for the online path?
  • How would you add 1 hour, 24 hour and 30 day versions without scanning the table four times?
  • How do you verify the SQL and the online feature produce the same numbers?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

One row per transaction; ts is UTC; status in ('ok', 'declined'). 'Prior 7 days' means ts - 7 days <= p.ts < t.ts. Do transactions at exactly the same timestamp count as prior? I will say no, si...

Approach

A self join on user with a time range condition, then aggregate per transaction. A LEFT JOIN keeps first transactions with zero counts. Conditional aggregation gives decline counts in the same pass. A...


Submit Your Answer
Markdown supported

Related Questions