How to GROUP BY Month and Year in SQLite?
Last Updated :
23 Jul, 2025
SQLite is a lightweight and server-less relational database management system. It requires very minimal configurations therefore it is easy to integrate into applications. It is also considered as an ideal choice for small mobile and desktop applications. In SQLite, GROUP BY month and year is considered useful when we are dealing with some time-based analysis.
It is also useful when dealing with other tasks like data visualization, forecasting, planning, etc. In this article, We will learn about How to GROUP BY Month and Year in SQLite by understanding the various methods along with the various examples and so on.
How to GROUP BY Month and Year in SQLite
In SQLite, we can easily perform GROUP BY month and year with the help of strftime() function. The strftime() function will help us to extract the month and year from the date entered in the table. Thus, we can perform the GROUP BY related operations accordingly.
Syntax:
SELECT column_name(s)
FROM table_name
GROUP BY STRFTIME(''%m-%Y'', column_name);
Explanation: This query selects columns from a table and groups the results by formatting a column containing dates into a string representing the month and year (for examples: '03-2024' for March 2024).
The STRFTIME
(
'%m-%Y', column_name
)
function is used for this formatting. The GROUP BY
clause then groups the results based on this formatted string, allowing for aggregation and analysis based on month and year.
Let's set up an Environment
To understand How to GROUP BY Month and Year in SQLite we need a table on which we will perform various operations and queries. Here we will consider a table called geeksforgeeks which contains id, name, questions, and submission_day as Columns.
After Inserting some data into the table the table looks:
Output:
Table - geeksforgeeksUsing strftime Function
Example 1: GROUP BY Month
In this example, we are going to perform GROUP BY operation with month. We are going to count number of fields from the table grouping them with month.
In simple words, we are going to calculate number of rows with respect to the month they belong.
Query:
SELECT STRFTIME('%m', submission_day) AS sumission_month, COUNT(*) as month_count
FROM geeksforgeeks
GROUP BY STRFTIME('%m', submission_day);
Output:
GROUP BY monthExplanation: We can see that all the months have their respective row count which represents the records which contains that month.
Example 2: GROUP BY Year
In this example, we are going to perform the same operation but this time we will group them with year. We are going to count all the records with respect to there years.
Query:
SELECT STRFTIME('%Y', submission_day) AS sumission_year, COUNT(*) as year_count
FROM geeksforgeeks
GROUP BY STRFTIME('%Y', submission_day);
Output:
GROUP BY yearExplanation: As we have seen in the previous example, all the years have their respective row count value.
Example 3: GROUP BY month along with year
In this example, we are going to group all the records with respect to their month but this time we will focus on their respective year too.
Query:
SELECT STRFTIME('%m-%Y', submission_day) AS sumission_month_year, COUNT(*) as month_year_count
FROM geeksforgeeks
GROUP BY STRFTIME('%m-%Y', submission_day);
Output:
GROUP BY month and yearExplanation: In the above image, we can clearly see that month and year has been displayed along with their respective row count value.
This represents the records which are present in that particular month and year. This time we have kept focus on both i.e. month and year.
Conclusion
Overall, to GROUP BY month and year is easily done with strftime() function. The strftime() function will help us to extract month and year from the previously inserted date. We have covered how we can perform GROUP BY related operations with month, year and both combined. Now you have a good understanding on performing GROUP BY operations with month and year. Now you can write queries related to it and can get the desired results.
Similar Reads
How to Group by Month and Year in MySQL? GROUP BY clause is an essential feature for data analysis. It enables users to aggregate information based on specific criteria. To group records based on day, month, and year, we can use DAY(), MONTH(), and YEAR() functions respectively. In this article, we will look at how to use MySQLâs GROUP BY
4 min read
How to Group by Day, Date, Hour, Month or Year in SQL Server Grouping data by day, date, hour, month, or year in SQL Server involves using the GROUP BY clause and appropriate date functions to extract or manipulate the relevant portions of the datetime column. The SQL Server GROUP BY operator is similar to the SQL GROUP BY Operator. The GROUP BY clause is a p
3 min read
How to Group or Ungroup Data in a PivotTable Pivot Table Grouping is a feature that enables us to organize data into specific categories or ranges. It allows you to group data based on various factors, such as dates, numbers, or custom-defined intervals, making it easier to analyze and draw insights from the data. We will explore Pivot table g
8 min read
How to Query as GROUP BY in Django? In Django, the powerful ORM (Object-Relational Mapping) allows developers to interact with databases using Python code. One common database operation is the GROUP BY query, which groups rows sharing a property so that aggregate functions can be applied to each group. This article will guide you thro
3 min read
How to Compare Product Sales By Month in SQL? A monthly sales report represents the state of sales activities in a company per month. It helps the sales team to align their efforts effectively. Whether we are a sales leader or manager, metrics play a crucial role in ensuring our company's success. If our data is stored in a database, SQL provid
4 min read
SQL Query to Convert an Integer to Year Month and Days With this article, we will be knowing how to convert an integer to Year, Month, Days from an integer value. The prerequisites of this article are you should be having a MSSQL server on your computer. What is a query? A query is a statement or a group of statements written to perform a specific task,
2 min read