How to Write a SQL Query to Group Sales by Country and Month

Analyzing sales data by both country and month can provide invaluable insights for businesses aiming to track performance trends and make data-driven decisions. If you’re tasked with generating such reports, knowing how to structure your SQL queries efficiently is essential. In this blog post, we’ll walk through the process of writing a SQL query to group sales by country and month, and show how AI2sql streamlines this task—even for those with limited SQL experience.

Why Group Sales Data by Country and Month?

Grouping sales data by country and month allows you to:

  • Spot geographic trends: Identify which countries are top performers over time.

  • Detect seasonal patterns: See how monthly sales fluctuate in different regions.

  • Optimize strategy: Use detailed breakdowns to refine your sales strategies and marketing efforts.

Sample Sales Table Structure

Before writing the query, let’s assume your sales data is stored in a table like sales with these columns:

  • id (unique sale identifier)

  • country (the country of sale)

  • sale_date (date of the sale)

  • amount (sale amount)

SQL Query to Group Sales by Country and Month

To group sales by country and by month, use SQL’s GROUP BY clause along with date manipulation functions. Here’s an example query:

SELECT 
  country,
  DATE_FORMAT(sale_date, '%Y-%m') AS sale_month,
  SUM(amount) AS total_sales
FROM sales
GROUP BY country, sale_month
ORDER BY country, sale_month;

Explanation:

  • DATE_FORMAT(sale_date, '%Y-%m') extracts the year and month from the sale date (syntax may vary by SQL flavor).

  • SUM(amount) totals sales per group.

  • GROUP BY country, sale_month creates groups by both country and month.

Output Example

CountrySale MonthTotal Sales
USA2024-0115,000
USA2024-0217,600
UK2024-018,200
UK2024-029,500

How AI2sql Can Help

Writing SQL queries can be challenging, especially if you’re unfamiliar with syntax or SQL dialect differences. AI2sql lets you simply describe your data needs in plain English, and instantly generates the right SQL statement for you.

Example: Text-to-SQL with AI2sql

  • Input: “Group sales by country and month and show the total sales amount.”

  • AI2sql Output:

SELECT country, DATE_FORMAT(sale_date, '%Y-%m') AS sale_month, SUM(amount) AS total_sales
FROM sales
GROUP BY country, sale_month
ORDER BY country, sale_month;

This tool speeds up analytics, reduces errors, and empowers both analysts and non-technical users to work with data more confidently.

Tips for Effective SQL Grouping

  • Confirm your date field’s format and use the correct SQL date function (e.g., DATE_TRUNC for PostgreSQL).

  • Always alias your columns for readability.

  • Use ORDER BY to keep results organized.

Additional Resources

Conclusion

Grouping sales by country and month in SQL provides deep insights and is simple with the right approach. Let AI2sql handle the heavy lifting, so you can focus on interpreting results and making impactful business decisions. Try AI2sql today to accelerate your analytics workflows!

Share this

TOOLS

Build Your Own AI Agent Team in 15 Min — Free OpenClaw Guide

Build Your Own AI Agent Team in 15 Min — Free OpenClaw Guide

Feb 5, 2026

TOOLS

OpenClaw AI Assistant: Local 24/7 Automation Guide 2026

OpenClaw AI Assistant: Local 24/7 Automation Guide 2026

Feb 4, 2026

TOOLS

SQL WITH Clause (CTE): Complete Guide with Examples

SQL WITH Clause (CTE): Complete Guide with Examples

Jan 14, 2026

TOOLS

MySQL to PostgreSQL Migration: Complete 2026 Guide with Syntax Conversion

MySQL to PostgreSQL Migration: Complete 2026 Guide with Syntax Conversion

Jan 14, 2026

TOOLS

SQL vs Excel: When Should You Make the Switch? [2026]

SQL vs Excel: When Should You Make the Switch? [2026]

Jan 14, 2026

Copyright © AI2sql 2026

Cross Regions Technology

13553 Atlantic Blvd, Suite 201

FL 32225

support@ai2sql.io

Company