For each customer ID, return the total number of orders they placed in 2025 where the order number is greater than 5.
💡 Model Answer
Assuming the orders table has columns customer_id, order_date, and order_number, you can filter by year and order number, then aggregate.
SELECT customer_id, COUNT(*) AS total_orders
FROM orders
WHERE YEAR(order_date) = 2025
AND order_number > 5
GROUP BY customer_id;
Explanation: YEAR(order_date) extracts the year part of the date; the WHERE clause keeps only rows from 2025 with order_number > 5. GROUP BY groups the remaining rows by customer_id, and COUNT(*) tallies how many qualifying orders each customer has. The query returns one row per customer with the count of qualifying orders. Complexity is O(n) for scanning the table once and grouping by key.
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 — invisible to screen sharing.
Get Assisting AI — Starts at ₹500