How to Order by Date in PostgreSQL or Oracle Database: PostgreSQL Oracle Operators: ORDER BY ASC DESC TO_DATE EXTRACT Table of Contents Problem: Example 1: Solution: Discussion: Problem: You want to sort the rows by date in PostgreSQL or Oracle database. Example 1: The exam table has two columns, subject and exam_date. subjectexam_date Mathematics2022-12-19 English2023-01-08 Science2023-01-05 Health2023-01-05 ArtNULL You want to sort the rows by exam_date. Solution: SELECT * FROM exam ORDER BY exam_date; The result looks like this (the rows are sorted in ascending order by exam_date): subjectexam_date Mathematics2022-12-19 Science2023-01-05 Health2023-01-05 English2023-01-08 ArtNULL Discussion: Use the ORDER BY keyword and the name of the column by which you want to sort. This way, you'll sort the data in ascending order by this column. You could also use the ASC keyword to make it clear that the order is ascending (the earliest date is shown first, the latest date is shown last, etc.). SELECT * FROM exam ORDER BY exam_date ASC; If you'd like to see the latest date first and the earliest date last, you need to sort in descending order. Use the DESC keyword in this case. SELECT * FROM exam ORDER BY exam_date DESC; Note that in PostgreSQL and in Oracle, NULLs are displayed last when sorting in ascending order and first when sorting in descending order. Also, the rows with the same exam_date are displayed in random order (you may see Science second and Health third, or Health second and Science third). Recommended courses: SQL Basics in PostgreSQL SQL Practice Set in PostgreSQL Recommended articles: PostgreSQL Cheat Sheet SQL Basics Cheat Sheet How ORDER BY and NULL Work Together in SQL The Most Useful Date and Time Functions What Does ORDER BY Do? How Long Does It Take to Learn SQL? 19 PostgreSQL Practice Exercises with Detailed Solutions Best Books for Learning PostgreSQL PostgreSQL Date Functions See also: How to Convert a String to a Date in PostgreSQL How to Order By Two Columns in SQL? How to Order Alphabetically in SQL How to Calculate Date Difference in PostgreSQL/Oracle How to Get Yesterday’s Date in PostgreSQL How to Extract the Week Number from a Date in PostgreSQL Subscribe to our newsletter Join our monthly newsletter to be notified about the latest posts. Email address How Do You Write a SELECT Statement in SQL? What Is a Foreign Key in SQL? Enumerate and Explain All the Basic Elements of an SQL Query