Back to cookbooks list Articles Cookbook

How to Order by Date in SQLite

Problem:

You want to sort the rows by date.

Example 1:

The exam table has two columns, subject and exam_date.

subjectexam_date
Mathematics2019-12-19
English2020-01-08
Science2020-01-05
Health2020-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):

SubjectExamDate
ArtNULL
Mathematics2019-12-19
Science2020-01-05
Health2020-01-05
English2020-01-08

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 SQLite, NULLs are displayed first when sorting in ascending order and last 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).

Example 2:

The exam table has the following columns: subject, exam_year, exam_month, and exam_day. The months are given in names, not in numbers.

subjectexam_yearexam_monthexam_day
Mathematics2019December19
English2020January8
Science2020January5
Health2020January5
ArtNULLNULLNULL

Solution:

SELECT *
FROM exam
ORDER BY exam_year,
 (CASE exam_month
    WHEN 'January' THEN 1
    WHEN 'February' THEN 2
    WHEN 'March' THEN 3
    WHEN 'April' THEN 4
    WHEN 'May' THEN 5
    WHEN 'June' THEN 6
    WHEN 'July' THEN 7
    WHEN 'August' THEN 8
    WHEN 'September' THEN 9
    WHEN 'October' THEN 10
    WHEN 'November' THEN 11
    WHEN 'December' THEN 12
  END),
  exam_day;

The result looks like this (the rows are sorted in ascending order by exam_year, exam_month, and exam_day):

subjectexam_yearexam_monthexam_day
ArtNULLNULLNULL
Mathematics2019December19
Health2020January5
Science2020January5
English2020January8

Discussion:

To sort the rows by exam date, you need to sort first by year, then by numerical month (not month name), and finally by day. You can convert month names to numerical months with a CASE WHEN clause. After the CASE keyword, specify the name of the column. Then, after each WHEN, state the value in this column, use the THEN keyword, and specify the new value you'd like to assign instead of the old one. Here, the column is exam_month, the current values in this column are 'January', 'February', …, 'December', and the new values are the numerical months 1, 2, …, 12. After you finish converting all the values, remember to use the END keyword to close the CASE WHEN clause. Take a look:

CASE exam_month
  WHEN 'January' THEN 1
  WHEN 'February' THEN 2
  WHEN 'March' THEN 3
  WHEN 'April' THEN 4
  WHEN 'May' THEN 5
  WHEN 'June' THEN 6
  WHEN 'July' THEN 7
  WHEN 'August' THEN 8
  WHEN 'September' THEN 9
  WHEN 'October' THEN 10
  WHEN 'November' THEN 11
  WHEN 'December' THEN 12
END

This is how you convert a month name to a month number. You can use it when sorting the rows by date, that is, by year, numerical month, and day.

ORDER BY exam_year,
 (CASE exam_month
    WHEN 'January' THEN 1
    WHEN 'February' THEN 2
    WHEN 'March' THEN 3
    WHEN 'April' THEN 4
    WHEN 'May' THEN 5
    WHEN 'June' THEN 6
    WHEN 'July' THEN 7
    WHEN 'August' THEN 8
    WHEN 'September' THEN 9
    WHEN 'October' THEN 10
    WHEN 'November' THEN 11
    WHEN 'December' THEN 12
  END),
  exam_day

This way, you can sort the rows in ascending order by date. The NULLs will be displayed first. To change the order to descending, use the DESC keyword after each column in the ORDER BY clause. Here's what the whole query should look like:

SELECT *
FROM exam
ORDER BY
  exam_year DESC,
  (CASE exam_month
    WHEN 'January' THEN 1
    WHEN 'February' THEN 2
    WHEN 'March' THEN 3
    WHEN 'April' THEN 4
    WHEN 'May' THEN 5
    WHEN 'June' THEN 6
    WHEN 'July' THEN 7
    WHEN 'August' THEN 8
    WHEN 'September' THEN 9
    WHEN 'October' THEN 10
    WHEN 'November' THEN 11
    WHEN 'December' THEN 12
  END) DESC,
  exam_day DESC;

Note that when sorting in descending order in SQLite, NULLs are displayed last.

Recommended courses:

Recommended articles:

See also: