Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Order By Date in SQL

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];
  • ASC sorts dates from oldest to newest (default).

  • DESC sorts 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?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql