๐ง SQL Interview Question (Detect Negative Account Balance)
๐
transactions(txn_id, txn_date, amount)
(credit = +ve, debit = -ve)
โ Ques :
๐ Find the first date when account balance becomes negative
๐ Return txn_date
๐งฉ How Interviewers Expect You to Think
โข Calculate running balance over time ๐ฐ
โข Use cumulative sum
โข Track when balance drops below zero
โข Return first occurrence
๐ก SQL Solution
WITH balance_cte AS (
SELECT
txn_date,
SUM(amount) OVER (
ORDER BY txn_date
) AS running_balance
FROM transactions
)
SELECT txn_date
FROM balance_cte
WHERE running_balance < 0
ORDER BY txn_date
LIMIT 1;
๐ฅ Why This Question Is Powerful
โข Tests cumulative sum (window function) ๐ง
โข Very common in fintech & transaction analysis
โข Checks real-world problem solving ability
โค๏ธ React for more SQL interview questions ๐
Post #2131
2.27K
- โค 5