Working with dates in SQL
Dates compare, sort and group like numbers once you know their format.
SQL skills · Functions
What you will learn
In the practice tables dates are stored as text in the form YYYY-MM-DD, which sorts correctly.
You will filter ranges, take out the month or year and count per month.
The basics
In these practice tables dates are text in the form YYYY-MM-DD. In that form, text order is date order, so =, <, > and BETWEEN work as expected.
SELECT customer, due_date FROM invoices WHERE due_date BETWEEN '2026-03-01' AND '2026-03-31'Parts of a date
SELECT MONTH(due_date) AS month, COUNT(*)
FROM invoices
GROUP BY MONTH(due_date)
ORDER BY monthYEAR, MONTH and DAY work in SQL Server, MySQL and Snowflake. PostgreSQL and Oracle use EXTRACT(MONTH FROM due_date); both forms work here.
Adding days and counting days between dates is different in every database, so it is taught in the dialect tracks.
Worked examples
Example 1
Orders per month, oldest first.
SELECT MONTH(order_date) AS month, COUNT(*)
FROM orders
GROUP BY MONTH(order_date)
ORDER BY monthPostgreSQL and Oracle write EXTRACT(MONTH FROM order_date).
Reading helps, typing makes it stick. Every task gives you a fresh table, and your query is checked by a real database.
Try a taskCommon mistakes
- Writing dates as 03/05/2026, which does not sort correctly as text.
- Forgetting quotes around a date.
- Grouping by month across several years without the year.
Where you will use it at work
- Finance: invoices due this quarter.
- HR: hires per year.
- Support: tickets opened per month.
Questions people ask
How do I add 30 days to a date?
It differs by database: DATEADD in SQL Server and Snowflake, date + 30 in Oracle and PostgreSQL. See the dialect tracks.
What about times?
With date and time values, a range ending on a day should use < the next day.
What is today’s date in SQL?
CURRENT_DATE in most databases, GETDATE() in SQL Server.
Ready to write it yourself?
Practice Dates