You have 2 minutes to solve this SQL query.
Q: Find the customer(s) who placed orders in every month of the year 2025.
Assume the table structure:
orders(order_id, customer_id, order_date)
๐ ๐ฒ: Challenge accepted! ๐ช
SELECT
customer_id
FROM orders
WHERE YEAR(order_date) = 2025
GROUP BY customer_id
HAVING COUNT(DISTINCT MONTH(order_date)) = 12;
๐ก Explanation:
This query identifies customers who placed at least one order in every month of 2025.
โข
WHERE YEAR(order_date) = 2025 filters orders from the year 2025โข
GROUP BY customer_id groups all orders by customerโข
COUNT(DISTINCT MONTH(order_date)) counts the unique months in which each customer placed an orderโข
HAVING ... = 12 ensures the customer has orders in all 12 monthsThis question tests your understanding of:
โ Date Functions (YEAR, MONTH)
โ GROUP BY
โ HAVING
โ COUNT(DISTINCT)
๐ฏ Expected Output Example
| Customer ID |
|-------------|
| 101 |
| 205 |
These customers placed at least one order in every month of 2025.
๐ Alternative (Database-Agnostic SQL)
SELECT
customer_id
FROM orders
WHERE EXTRACT(YEAR FROM order_date) = 2025
GROUP BY customer_id
HAVING COUNT(DISTINCT EXTRACT(MONTH FROM order_date)) = 12;
This version works with databases like PostgreSQL and Oracle that support the
EXTRACT() function.๐ Tip for SQL Job Seekers:
Whenever you see interview questions containing phrases like:
"Every month" / "Every quarter" / "Every year" / "Every category"
Think of
COUNT(DISTINCT ...) combined with GROUP BY and HAVING. This is a very common SQL interview pattern.โค๏ธ React with โค๏ธ for more interview challenges!