We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium sql problem, graded against 3 test cases (1 of them hidden).
Query-writing problems - joins, aggregation, window functions, and query tuning.
Reach for it when you see: A schema and a question about the data rather than a function signature.
More SQLproblems →```sql
SELECT TO_CHAR(DATE_TRUNC('month', order_date), 'YYYY-MM') AS month,
ROUND(AVG(total)::numeric, 2) AS avg_total
FROM Orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY month;
```
MySQL equivalent: `DATE_FORMAT(order_date, '%Y-%m')`. SQL Server: `FORMAT(order_date, 'yyyy-MM')` or `DATEPART(year/month, ...)`.
Performance trap: functions on the grouping column block index usage if you GROUP BY `DATE_TRUNC` directly. For very large tables, store a generated `order_month` column with an index, or use range filters per month and UNION ALL.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.