What is wrong with this query, and how would you fix it?
💡 Model Answer
The snippet you posted has a few common SQL syntax mistakes. First, the CTE (Common Table Expression) must start with the WITH keyword; without it the parser will throw an error. Second, in the SELECT list of the CTE you omitted a comma after the amount column, so the parser treats 'amount status' as a single identifier. Third, you referenced transaction_date in the outer SELECT but never included it in the CTE, which will also cause an error. A corrected version looks like this: WITH ranked_transactions AS ( SELECT customer_id, amount, status, transaction_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY transaction_date DESC) AS rn FROM transactions WHERE status = 'success' ) SELECT customer_id, amount, transaction_date FROM ranked_transactions WHERE rn = 1; This query uses a CTE, adds the missing comma, includes transaction_date in the CTE, and filters for the most recent successful transaction per 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