Home β€Ί Interview Questions β€Ί Write a SQL query that returns each customer ID an…

Write a SQL query that returns each customer ID and the total number of orders they placed during the year 2025, including only customers whose total orders exceed five.

🟑 Medium Coding Junior level
1Times asked
May 2026Last seen
May 2026First seen

πŸ’‘ Model Answer

The query is identical to the first one; the key is filtering by the year and applying a HAVING clause:

SELECT customer_id,

   COUNT(*) AS total_orders

FROM Orders

WHERE order_date >= '2025-01-01' AND order_date < '2026-01-01'

GROUP BY customer_id

HAVING COUNT(*) > 5;

Using a range on order_date is often more efficient than a function on the column, allowing the database to use an index on order_date if available.

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