How to Order by Month Name in PostgreSQL or Oracle Database: PostgreSQL Oracle Operators: ORDER BY ASC DESC TO_DATE EXTRACT Table of Contents Problem: Example: Solution: Discussion: Problem: You want to sort the rows by month number, given month names (you want January to be shown first, December last). Example: The birthday table contains two columns: name and birthday_month. The months are given in names, not in numbers. namebirthday_month Ronan TishaNULL Angie JuliaApril Narelle DillanApril Purdie CaseyJanuary Donna NellNULL Blaze GraemeOctober You want to sort the rows by birthday_month. Solution: SELECT * FROM birthday ORDER BY EXTRACT(MONTH FROM TO_DATE(birthday_month, 'Month')); The result looks like this (the rows are sorted in ascending order by birthday_month): namebirthday_month Purdie CaseyJanuary Angie JuliaApril Narelle DillanApril Blaze GraemeOctober Ronan TishaJanuary Donna NellNULL Discussion: To sort the rows by month starting from January and ending with December, you need to convert the month to a number (January to 1, February to 2, etc.). Otherwise, you would see December before January. The TO_DATE(birthday_month, 'Month') function converts a full month name to a date in the 0001-MM-01 format. For example, you get 0001-12-01 for December. You can now use the EXTRACT(MONTH FROM date) function to extract the month from this date value. The month will be a number between 1 and 12. Combining these two functions, you can get the month as a number using the following formula: EXTRACT(MONTH FROM TO_DATE(birthday_month, 'Month')) Put this expression after ORDER BY to sort the rows by month. If you'd like to see the latest month first, you'll need to sort in descending order. To do this, you need to use a DESC keyword, like this: SELECT * FROM birthday ORDER BY EXTRACT(MONTH FROM TO_DATE(birthday_month, 'Month')) DESC; Note that in PostgreSQL and Oracle, NULLs are displayed last when sorting in ascending order and first when sorting in descending order. Also, the rows with the same birthday_month are displayed in random order (you may see Angie Julia second and Narelle Dillan third, or Narelle Dillan second and Angie Julia third). Recommended courses: SQL Basics SQL Practice Set Standard SQL Functions Recommended articles: PostgreSQL Cheat Sheet SQL for Data Analysis 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