Join our telegram channel to learn how data analysis can reveal fascinating patterns, trends, and stories hidden within the numbers! 📊
For ads & suggestions: @love_data
Post #2076
1.68K
🧠 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
📌
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
- ❤ 5
- 👌 1















