Order By Date in SQL
ORDER BY Date in SQL
You can use the ORDER BY clause to sort query results based on date columns. This is useful to get rows sorted by date in ascending (oldest first) or descending (newest first) order.
Syntax:
SELECT column1, column2, date_columnFROM table_nameORDER BY date_column [ASC | DESC];ASCsorts dates from oldest to newest (default).DESCsorts dates from newest to oldest.
Examples:
1. Order by date ascending (oldest first)
SELECT event_name, event_dateFROM eventsORDER BY event_date ASC;2. Order by date descending (newest first)
SELECT event_name, event_dateFROM eventsORDER BY event_date DESC;Notes:
You can order by any date/time column (
DATE,DATETIME,TIMESTAMP).You can combine with other columns for sorting, e.g.:
ORDER BY event_date DESC, event_name ASC;Make sure the column is stored in a date or datetime data type for proper sorting.
Want examples on how to extract date parts (like year or month) and sort by those?