๐ง SQL Interview Question (Commonly Asked)
๐
orders(order_id, customer_id, order_date, order_amount)
โ Ques :
๐ Find customers whose order amount strictly increased compared to their previous order.
๐งฉ How Interviewers Expect You to Think
โข Order data correctly using dates
โข Compare current row with previous row
โข Use window functions for self-comparison
โข Avoid self-joins when window functions fit better
โข Filter only after comparison logic
๐ก SQL Solution
WITH ranked_orders AS (
SELECT
customer_id,
order_date,
order_amount,
LAG(order_amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS prev_order_amount
FROM orders
)
SELECT DISTINCT
customer_id
FROM ranked_orders
WHERE order_amount > prev_order_amount;
๐ฅ React โฅ๏ธ if you want more moderate-to-advanced SQL interview questions
Post #2076
1.68K
- โค 5
- ๐ 1