TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2943 4.5K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
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 months

This 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!
  • โค 15
More from @sqlspecialist
  1. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  2. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  3. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
  6. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 4 Guys, let's continue our Data Analyst Interviewโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’