๐ง SQL Interview Question (ModerateโTricky & Identifying Users with Increasing Transactions)
๐
transactions(transaction_id, user_id, transaction_date, amount)
โ Ques :
๐ Find users whose transaction amount strictly increases with every new transaction.
๐งฉ How Interviewers Expect You to Think
โข Sort transactions by date for each user
โข Compare each amount with the previous one
โข Identify users whose amounts always increase
๐ก SQL Solution
WITH t AS (
SELECT
user_id,
amount,
LAG(amount) OVER (
PARTITION BY user_id
ORDER BY transaction_date
) AS prev_amount
FROM transactions
)
SELECT user_id
FROM t
GROUP BY user_id
HAVING SUM(
CASE
WHEN prev_amount IS NOT NULL AND amount <= prev_amount
THEN 1 ELSE 0
END
) = 0;
๐ฅ Why This Question Is Powerful
โข Tests understanding of LAG() with conditional logic
โข Evaluates ability to validate patterns across sequential data
โข Reflects real-world analytics like tracking user spending growth trends
โค๏ธ React if you want more tricky real interview-level SQL questions ๐
Post #2104
1.68K
- โค 4