Previously, I wrote an article on how to group records by year, month, and day in PostgreSQL — if you haven’t seen it yet, you can check it out here.
In this follow-up, we’ll explore how to achieve the same results in MySQL. Using a sample orders
table, we’ll go through different ways to group data by year, month, and day using functions like DATE_FORMAT()
, EXTRACT()
, YEAR()
, MONTH()
, DAY()
, and more.
Whether you’re building reports, dashboards, or just exploring your data, these techniques will help you slice it by time with ease.
Before digging deeper, let’s consider that you have the following orders
table:
id | price | createdAt |
---|---|---|
1 | 100.00 | 2022-01-12 01:15:00 |
2 | 120.00 | 2022-02-10 23:44:00 |
3 | 80.00 | 2023-01-09 08:14:00 |
4 | 150.00 | 2023-07-09 20:50:00 |
5 | 50.00 | 2023-07-10 09:33:00 |
6 | 30.00 | 2023-07-10 15:23:00 |
7 | 90.00 | 2023-09-23 10:12:00 |
And your manager asks you three requirements:
- Calculate the total sales every year.
- Calculate the total sales every month in 2023.
- Calculate the total sales every day in July 2023.
To calculate the total sales per year:
- Using
DATE_FORMAT()
SELECT DATE_FORMAT(createdAt, '%Y') AS year, SUM(price) AS total
FROM orders
GROUP BY year
ORDER BY year;
This statement returns:
year | total |
---|---|
2022 | 220.00 |
2023 | 400.00 |
- Using
EXTRACT()
SELECT EXTRACT(YEAR FROM createdAt) AS year, SUM(price) AS total
FROM orders
GROUP BY year
ORDER BY year;
And it returns the same result.
- Using
YEAR()
SELECT YEAR(createdAt) AS year, SUM(price) AS total
FROM orders
GROUP BY year
ORDER BY year;
Also, returns the same output.
To calculate the total sales per month in 2023:
- Using
DATE_FORMAT()
SELECT DATE_FORMAT(createdAt, '%Y-%m') AS `year_month`, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023
GROUP BY `year_month`
ORDER BY `year_month`;
This query returns:
year_month | total |
---|---|
2023-01 | 80.00 |
2023-07 | 230.00 |
2023-09 | 90.00 |
💌 Enjoying this post? Subscribe to get more practical dev tips right in your inbox.
- Using
EXTRACT()
SELECT EXTRACT(MONTH FROM createdAt) AS month, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023
GROUP BY month
ORDER BY month;
This query returns:
month | total |
---|---|
1 | 80.00 |
7 | 230.00 |
9 | 90.00 |
- Using
MONTH()
SELECT MONTH(createdAt) AS month, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023
GROUP BY month
ORDER BY month;
And this returns the same previous result.
To calculate the total sales per day in July 2023:
- Using
DATE_FORMAT()
SELECT DATE_FORMAT(createdAt, '%Y-%m-%d') AS date, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY date
ORDER BY date;
This query returns:
date | total |
---|---|
2023-07-09 | 150.00 |
2023-07-10 | 80.00 |
- Using
DATE()
SELECT DATE(createdAt) AS day_date, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY day_date
ORDER BY day_date;
The same result.
- Using
EXTRACT()
SELECT EXTRACT(DAY FROM createdAt) AS day, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY day
ORDER BY day;
This query returns:
day | total |
---|---|
9 | 150.00 |
10 | 80.00 |
- Using
DAY()
SELECT DAY(createdAt) AS day, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY day
ORDER BY day;
And this returns the same previous result.
In this article, we learned how to group data by a specific date interval, year, month, and day.
And to do so, we used some functions:
DATE_FORMAT()
EXTRACT()
YEAR()
,MONTH()
,DAY()
, andDATE()
If you enjoyed this article, I’d truly appreciate it if you could share it—it really motivates me to keep creating more helpful content!
If you’re interested in exploring more, check out these articles.
- MongoDB GridFS Made simple
- Isolation Levels In SQL Server With Examples
- 4 Ways To Handle Asynchronous JavaScript
Thanks for sticking with me until the end—I hope you found this article valuable and enjoyable!
Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.