HomeInterview QuestionsIn SQL, suppose you have a table with customer ID,…

In SQL, suppose you have a table with customer ID, transaction date, and amount. How would you find the latest transaction for each customer? Explain your approach.

🟡 Medium Coding Junior level
1Times asked
Aug 2026Last seen
Aug 2026First seen

💡 Model Answer

To get the latest transaction per customer you can use a window function or a correlated subquery. Example with window function: SELECT customer_id, transaction_date, amount FROM ( SELECT t., ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY transaction_date DESC) AS rn FROM transactions t ) sub WHERE rn = 1; This assigns a row number per customer ordered by date descending and keeps only the first row. Complexity is O(n) time and O(n) space for the window. Alternatively, a correlated subquery: SELECT t. FROM transactions t WHERE t.transaction_date = (SELECT MAX(transaction_date) FROM transactions WHERE customer_id = t.customer_id); This runs in O(n log n) if indexes exist. Both approaches return the most recent transaction for each customer.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500