TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2921 4.88K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.
Find the month with the highest total sales.

Assume the table structure: sales(sale_id, sale_date, amount)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช
SELECT
EXTRACT(YEAR FROM sale_date) AS year,
EXTRACT(MONTH FROM sale_date) AS month,
SUM(amount) AS total_sales
FROM sales
GROUP BY
EXTRACT(YEAR FROM sale_date),
EXTRACT(MONTH FROM sale_date)
ORDER BY total_sales DESC
LIMIT 1;

๐Ÿ’ก Explanation:
This query groups sales by year and month, calculates the total sales for each month, and returns the month with the highest sales.

Key parts:
โ€ข EXTRACT YEAR FROM sale_date gets the year.
โ€ข EXTRACT MONTH FROM sale_date gets the month.
โ€ข SUM amount calculates total monthly sales.
โ€ข ORDER BY total_sales DESC sorts from highest to lowest.
โ€ข LIMIT 1 returns the top-performing month.

This question tests your understanding of:
Date Functions, Aggregate Functions SUM, GROUP BY, ORDER BY

๐ŸŽฏ Expected Output Example
Year: 2026, Month: 5, Total Sales: 245,000

๐Ÿš€ Alternative Handles Ties
SELECT
year,
month,
total_sales
FROM (
SELECT
EXTRACT(YEAR FROM sale_date) AS year,
EXTRACT(MONTH FROM sale_date) AS month,
SUM(amount) AS total_sales,
DENSE_RANK() OVER (
ORDER BY SUM(amount) DESC
) AS rnk
FROM sales
GROUP BY
EXTRACT(YEAR FROM sale_date),
EXTRACT(MONTH FROM sale_date)
) ranked
WHERE rnk = 1;

This version returns all months tied for the highest total sales.

๐Ÿš€ Date-based aggregation questions are among the most common in SQL interviews. Practice grouping data by Day, Week, Month, Quarter, Year. You'll encounter these patterns frequently in analytics and reporting roles.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
  • โค 13
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 โ†’