By month and by week
By month: GROUP BY DATE_FORMAT(order_date, '%Y-%m'). Average order value = sales ÷ orders. Months with no orders don't appear in the result. By week: DATE_SUB(d, INTERVAL WEEKDAY(d) DAY) gives the Monday of that week.
The health check is done, and here comes the boss's first question: "How did each month go this year?" This is the most common report there is: you take the orders, one by one, and gather them into one row of numbers per month.
Totals by month
In this step you group the orders by month. DATE_FORMAT(order_date, '%Y-%m') turns 2025-03-14 into 2025-03, so every order in the same month gets the same value, and you group by it. Remember to count only paid orders:
1SELECT DATE_FORMAT(order_date, '%Y-%m') AS month,2 COUNT(*) AS orders,3 SUM(amount) AS sales4FROM orders5WHERE status = 'paid'6GROUP BY month7ORDER BY month;
| month | orders | sales |
|---|---|---|
| 2025-01 | 1 | 36 |
| 2025-03 | 7 | 366 |
| 2025-04 | 2 | 126 |
| 2025-05 | 11 | 743 |
| 2025-06 | 5 | 310 |
| 2025-07 | 13 | 820 |
| 2025-08 | 11 | 626 |
| 2025-09 | 16 | 977 |
| 2025-10 | 12 | 789 |
| 2025-11 | 21 | 1042 |
| 2025-12 | 16 | 588 |
November is the busiest month: 21 orders and 1042 in sales. That's the big November sales, like Singles' Day and Black Friday.
| order_date | amount |
|---|---|
| 2025-11-02 | 36 |
| 2025-11-05 | 25 |
| … | … |
| 2025-12-04 | 24 |
| … | … |
GROUP BY DATE_FORMAT(order_date, '%Y-%m')| month | orders | sales |
|---|---|---|
| 2025-11 | 21 | 1042 |
| 2025-12 | 16 | 588 |
Other databases: DATE_FORMAT is how MySQL does it. In PostgreSQL you write TO_CHAR(order_date, 'YYYY-MM') or DATE_TRUNC('month', order_date), and in SQL Server FORMAT(order_date, 'yyyy-MM').
Why group by DATE_FORMAT(order_date, '%Y-%m') instead of simply MONTH(order_date)?
MONTH only gives the month, 1 to 12, whatever the year. The data here only covers 2025, so you can't see the problem yet. But once next year's data arrives, the two Januaries get mixed together. '%Y-%m' includes the year, so nothing gets mixed.A missing month
In this step you meet a trap that's easy to miss. Count the rows of the monthly result: January, March, April… 11 rows in all. February is gone.
February has no paid orders at all. GROUP BY can only group rows that exist, so a month with no orders simply doesn't appear. It doesn't show up as 0 either. On a line chart, January joins straight to March, and nobody notices February was empty.
1SELECT COUNT(DISTINCT DATE_FORMAT(order_date, '%Y-%m')) AS months2FROM orders3WHERE status = 'paid';
| months |
|---|
| 11 |
To fill February in as 0, you first need a table of all 12 months, then LEFT JOIN the orders to it. Lesson 10 shows how to make that table with WITH RECURSIVE. For now, just remember: when you group by time, check that no time periods are missing.
From January 1 to January 7, the shop had orders on only 3 of the days. If you group by day and count orders, how many rows does the result have?
1SELECT order_date, COUNT(*) AS n2FROM orders3WHERE order_date BETWEEN '2025-01-01' AND '2025-01-07'4GROUP BY order_date;5-- the result has rows
GROUP BY has nothing to group. Only the 3 days with orders get a row each.Average order value and buyers
In this step you work out two numbers bosses often ask for. Average order value is how much one order is worth on average: sales ÷ orders, which is just AVG(amount). Buyers is how many people bought something that month: someone who bought twice still counts once, so use COUNT(DISTINCT user_id).
1SELECT DATE_FORMAT(order_date, '%Y-%m') AS month,2 COUNT(*) AS orders,3 SUM(amount) AS sales,4 ROUND(AVG(amount), 1) AS avg_order5FROM orders6WHERE status = 'paid'7GROUP BY month8ORDER BY month;
| month | orders | sales | avg_order |
|---|---|---|---|
| 2025-01 | 1 | 36 | 36.0 |
| 2025-03 | 7 | 366 | 52.3 |
| 2025-04 | 2 | 126 | 63.0 |
| 2025-05 | 11 | 743 | 67.5 |
| 2025-06 | 5 | 310 | 62.0 |
| 2025-07 | 13 | 820 | 63.1 |
| 2025-08 | 11 | 626 | 56.9 |
| 2025-09 | 16 | 977 | 61.1 |
| 2025-10 | 12 | 789 | 65.8 |
| 2025-11 | 21 | 1042 | 49.6 |
| 2025-12 | 16 | 588 | 36.8 |
November has the most orders, yet the average order value fell from 65.8 in October to 49.6, and in December it's even lower, just 36.8. More orders, but smaller ones: during the sales, many people probably bought only small, cheap things. You'd never see this from sales alone.
Buyers in the last three months
✓For October, November and December, find the number of paid orders and the number of buyers each month. Three columns: month (written like 2025-10), orders and buyers, sorted by month.
| month | orders | buyers |
|---|---|---|
| 2025-10 | 12 | 11 |
| 2025-11 | 21 | 18 |
| 2025-12 | 16 | 15 |
1SELECT DATE_FORMAT(order_date, '%Y-%m') AS month2FROM orders3WHERE status = 'paid'4 AND order_date >= '2025-10-01'5GROUP BY month6ORDER BY month;
Which day of the week sells most
In this step you look from another angle: group by day of the week instead of by month. DAYNAME gives the English name of the day, and WEEKDAY gives a number: Monday is 0 and Sunday is 6.
1SELECT DAYNAME(order_date) AS day, COUNT(*) AS orders2FROM orders3WHERE status = 'paid'4GROUP BY day, WEEKDAY(order_date)5ORDER BY WEEKDAY(order_date);
| day | orders |
|---|---|
| Monday | 16 |
| Tuesday | 16 |
| Wednesday | 6 |
| Thursday | 12 |
| Friday | 24 |
| Saturday | 22 |
| Sunday | 19 |
Friday and Saturday have the most orders, and Wednesday has only 6. If you want to run a promotion, just before the weekend is a good time.
Why sort by WEEKDAY and not by day? day is text, so it sorts alphabetically: Friday, Monday, Saturday… and the days of the week are all jumbled. Sorting by the WEEKDAY number gives Monday to Sunday.
If you change the sort to ORDER BY day, which day is in the first row?
1SELECT DAYNAME(order_date) AS day, COUNT(*) AS orders2FROM orders3WHERE status = 'paid'4GROUP BY day5ORDER BY day;
day is text, so it sorts alphabetically. F comes first, so Friday is first. For Monday to Sunday, sort by WEEKDAY(order_date).How long until the first order
In this step you work out how many days lie between two dates. First, use WITH to find the day of each person's first paid order. Then compare it with their sign-up date. DATEDIFF(later, earlier) gives the number of days between them:
1WITH first_orders AS (2 SELECT user_id, MIN(order_date) AS first_order3 FROM orders4 WHERE status = 'paid'5 GROUP BY user_id6)7SELECT u.name, u.signup_date, f.first_order,8 DATEDIFF(f.first_order, u.signup_date) AS days9FROM users AS u10JOIN first_orders AS f ON f.user_id = u.id11ORDER BY days DESC, u.id12LIMIT 5;
| name | signup_date | first_order | days |
|---|---|---|---|
| Carla | 2025-01-12 | 2025-05-19 | 127 |
| Vera | 2025-06-23 | 2025-10-09 | 108 |
| Paula | 2025-04-03 | 2025-07-14 | 102 |
| Gabi | 2025-02-04 | 2025-05-03 | 88 |
| Emil | 2025-08-17 | 2025-11-10 | 85 |
Carla signed up in January but didn't order until May: 127 days later. She's the kind of user who signed up but wasn't won over. A discount coupon early on might have got her to order much sooner.
People who ordered within a week
✓Find the users whose first paid order came within 7 days of signing up (day 7 included). Two columns: name and days, sorted by days from smallest to largest, with ties sorted by user id.
| name | days |
|---|---|
| Will | 1 |
| Jade | 2 |
| Tomas | 3 |
| Hana | 3 |
| Olga | 5 |
| Mel | 7 |
1WITH first_orders AS (2 SELECT user_id, MIN(order_date) AS first_order3 FROM orders4 WHERE status = 'paid'5 GROUP BY user_id6)7SELECT u.name8FROM users AS u9JOIN first_orders AS f ON f.user_id = u.id;
By week
In this step you group by week. In a weekly report, "the week of December 1" is easier to read than "week 49". The trick is to move each date back to the Monday of its week: WEEKDAY tells you which day of the week it is (Monday is 0), and going back that many days lands on Monday.
1SELECT DATE_SUB(order_date, INTERVAL WEEKDAY(order_date) DAY) AS week_start,2 COUNT(*) AS orders,3 SUM(amount) AS sales4FROM orders5WHERE status = 'paid'6 AND order_date >= '2025-12-01'7GROUP BY week_start8ORDER BY week_start;
| week_start | orders | sales |
|---|---|---|
| 2025-12-01 | 2 | 60 |
| 2025-12-08 | 4 | 126 |
| 2025-12-15 | 8 | 288 |
| 2025-12-22 | 2 | 114 |
The week of December 15 is the busiest, with 8 orders. All 7 days of a week move back to the same Monday, so they land in the same group.
Tip: MySQL also has WEEK() and YEARWEEK(), but whether a week starts on Sunday or Monday, and which year the first days of January belong to, depends on the second argument, and it's easy to get wrong. "Move back to Monday" is clear at a glance.
Put these lines in order to make a query that runs: count the paid orders in each week of November, with each week shown as its Monday.
FROM ordersGROUP BY week_startORDER BY week_start;SELECT DATE_SUB(order_date, INTERVAL WEEKDAY(order_date) DAY) AS week_start, COUNT(*) AS ordersWHERE status = 'paid' AND order_date BETWEEN '2025-11-01' AND '2025-11-30'
week_start | orders 2025-10-27 | 1 2025-11-03 | 7 2025-11-10 | 7 2025-11-17 | 1 2025-11-24 | 5
What you learned
- To group by month, use
DATE_FORMAT(d, '%Y-%m'). It includes the year, so different years never get mixed. - Months or days with no data don't appear in a
GROUP BYresult. Check for yourself that no periods are missing. - Average order value = sales ÷ orders, which is
AVG(amount). Count buyers withCOUNT(DISTINCT user_id). Sales alone hide changes like "more orders, but smaller ones". - To group by day of the week, use
DAYNAME, and sort byWEEKDAY. DATEDIFF(later, earlier)gives the days between two dates. Find each person's first order withWITHfirst, then compare it with their sign-up date.- By week:
DATE_SUB(d, INTERVAL WEEKDAY(d) DAY)moves a date back to the Monday of its week.