SQL ORDER BY DATE

  • ORDER BY is a clause in SQL which is used with SELECT query to fetch the records in ascending or descending order from a table.
  • Just like we sort the integer and the string values stored in the column of the tables, similarly, we can sort the dates stored in the SQL table’s column.
  • All the records will be, by default, sorted in the ascending order. To sort the records in descending order, the DESC keyword is used.

Let us see few practical examples to understand this concept more clearly. We will use the MySQL database for writing all the queries.

Consider we have created a table named as employees in MySQL database with the following data:

IDNameSalaryJoining_DateDOB
1Rohit More500002020-02-081991-01-28 18:06:08
2Kunal Mohite340002021-01-011990-05-15 19:10:00
3Saurabh Jha610002015-05-011983-02-20 12:18:45
4Anant Desai590002018-08-271978-06-29 15:45:13
5Krishna Sharma480002010-10-231999-03-21 02:14:56
6Bhavesh Jain370002021-07-031998-08-02 13:00:01

Example 1:

Write a query to display all the details of employees arranged in ascending order of their date of birth.

Query:

mysql> SELECT *FROM employees ORDER BY DOB;  

Since we wanted to sort the records in the ascending order of the date of birth of employees, so we have applied the ORDER BY clause on the column ‘DOB’.

You will get the following output:

IDNameSalaryJoining_DateDOB
4Anant Desai590002018-08-271978-06-29 15:45:13
3Saurabh Jha610002015-05-011983-02-20 12:18:45
2Kunal Mohite340002021-01-011990-05-15 19:10:00
1Rohit More500002020-02-081991-01-28 18:06:08
6Bhavesh Jain370002021-07-031998-08-02 13:00:01
5Krishna Sharma480002010-10-231999-03-21 02:14:56

The results obtained from the above query show that the records are displayed according to the ascending order of the DOB.

Example 2:

Write a query to display all the details of employees arranged in descending order of their joining dates.

Query:

mysql> SELECT *FROM employees ORDER BY Joining_Date DESC;  

Since we wanted to sort the records in the descending order of the joining date of employees, so we have applied the ORDER BY clause with the DESC keyword on the column ‘Joining_Date’.

You will get the following output:

IDNameSalaryJoining_DateDOB
6Bhavesh Jain370002021-07-031998-08-02 13:00:01
2Kunal Mohite340002021-01-011990-05-15 19:10:00
1Rohit More500002020-02-081991-01-28 18:06:08
4Anant Desai590002018-08-271978-06-29 15:45:13
3Saurabh Jha610002015-05-011983-02-20 12:18:45
5Krishna Sharma480002010-10-231999-03-21 02:14:56

The results obtained from the above query show that the records are displayed according to the descending order of the joining dates.

Example 3:

Write a query to display all the details of employees arranged in ascending order of their year of birth.

Query:

mysql> SELECT ID, Name, Salary, Joining_Date, DATE_FORMAT (DOB, '%Y') AS Year_of_Birth FROM employees ORDER BY DATE_FORMAT (DOB, '%Y');  

Since we wanted to sort the records in the ascending order of the birth year of employees, so we have applied the ORDER BY clause. DATE_FORMAT () function is applied on the column DOB with the parameter ‘%Y’ to extract only the year from ‘DOB’.

You will get the following output:

IDNameSalaryJoining_DateYear_Of_Birth
4Anant Desai590002018-08-271978
3Saurabh Jha610002015-05-011983
2Kunal Mohite340002021-01-011990
1Rohit More500002020-02-081991
6Bhavesh Jain370002021-07-031998
5Krishna Sharma480002010-10-231999

The results obtained from the above query show that the records are displayed according to the ascending order of the employee’s year of birth.

Example 4:

Write a query to display all the details of employees arranged in descending order of their hour of birth.

Query:

mysql> SELECT ID, Name, Salary, Joining_Date, DATE_FORMAT (DOB, '%H') AS Hour_Of_Birth FROM employees ORDER BY DATE_FORMAT (DOB, '%H') DESC;  

Since we wanted to sort the records in the descending order of the birth hour of employees, so we have applied the ORDER BY clause with the DESC keyword. DATE_FORMAT () function is applied on the column DOB with the parameter ‘%H’ to extract only the hour of birth from the column’DOB’.

You will get the following output:

IDNameSalaryJoining_DateHour_Of_Birth
2Kunal Mohite340002021-01-0119
1Rohit More500002020-02-0818
4Anant Desai590002018-08-2715
6Bhavesh Jain370002021-07-0313
3Saurabh Jha610002015-05-0112
5Krishna Sharma480002010-10-2302

The results obtained from the above query show that the records are displayed according to the descending order of the employee’s hour of birth.

Example 5:

Write a query to display all the details of employees arranged in ascending order of their year of joining.

Query:

mysql> SELECT ID, Name, Salary, DOB, DATE_FORMAT (Joining_Date, '%Y') AS Year_Of_Joining FROM employees ORDER BY DATE_FORMAT (Joining_Date, '%Y');  

Since we wanted to sort the records in the ascending order of the joining year of employees, so we have applied the ORDER BY clause. DATE_FORMAT () function is applied on the column Joining_Date with the parameter ‘%Y’ to extract only the year from ‘Joining_Date’.

You will get the following output:

IDNameSalaryYear_Of_JoiningDOB
5Krishna Sharma4800020101999-03-21 02:14:56
3Saurabh Jha6100020151983-02-20 12:18:45
4Anant Desai5900020181978-06-29 15:45:13
1Rohit More5000020201991-01-28 18:06:08
2Kunal Mohite3400020211990-05-15 19:10:00
6Bhavesh Jain3700020211998-08-02 13:00:01

The results obtained from the above query show that the records are displayed according to the ascending order of the joining year of employees.

Example 6:

Write a query to display all the details of employees arranged in descending order of the joining day of employees.

Query:

mysql> SELECT ID, Name, Salary, DAY (Joining_Date) AS Day_Of_Joining, DOB FROM employees ORDER BY DAY (Joining_Date) DESC;  

Since we wanted to sort the records in the descending order of the joining day of employees, so we have applied the ORDER BY clause with the DESC keyword. DAY () function is applied on the column ‘Joining_Date’ to extract only the day of joining from Joining_Date.

You will get the following output:

IDNameSalaryDay_Of_JoiningDOB
4Anant Desai59000271978-06-29 15:45:13
5Krishna Sharma48000231999-03-21 02:14:56
1Rohit More5000081991-01-28 18:06:08
6Bhavesh Jain3700031998-08-02 13:00:01
2Kunal Mohite3400011990-05-15 19:10:00
3Saurabh Jha6100011983-02-20 12:18:45

The results obtained from the above query show that the records are displayed according to the descending order of the joining day of employees.

Example 7:

Write a query to display all the details of employees arranged in ascending order of the birth day of employees.

Query:

mysql> SELECT ID, Name, Salary, Joining_Date, DAY (DOB) AS Day_Of_Birth FROM employees ORDER BY DAY (DOB);  

Since we wanted to sort the records in the ascending order of the day of birth of employees, so we have applied the ORDER BY clause. DAY () function is applied on the column ‘DOB’ to extract only the day of birth from DOB.

You will get the following output:

IDNameSalaryYear_Of_JoiningDay_Of_Birth
6Bhavesh Jain370002021-07-032
2Kunal Mohite340002021-01-0115
3Saurabh Jha610002015-05-0120
5Krishna Sharma480002010-10-2321
1Rohit More500002020-02-0828
4Anant Desai590002018-08-2729

The results obtained from the above query show that the records are displayed according to the ascending order of the employee’s day of birth.

Example 8:

Write a query to display all the details of employees arranged in ascending order of the employee’s birth month.

Query:

mysql> SELECT ID, Name, Salary, Joining_Date, DOB FROM employees ORDER BY MONTH (DOB);  
IDNameSalaryJoining_DateDOB
1Rohit More500002020-02-081991-01-28 18:06:08
3Saurabh Jha610002015-05-011983-02-20 12:18:45
5Krishna Sharma480002010-10-231999-03-21 02:14:56
2Kunal Mohite340002021-01-011990-05-15 19:10:00
4Anant Desai590002018-08-271978-06-29 15:45:13
6Bhavesh Jain370002021-07-031998-08-02 13:00:01

The results obtained from the above query show that the records are displayed according to the ascending order of the employee’s birth month.

Example 9:

Write a query to display all the details of employees arranged in ascending order of the employee’s month of joining.

Query:

mysql> SELECT ID, Name, Salary, Joining_Date, DOB FROM employees ORDER BY MONTH (Joining_Date);  

Since we wanted to sort the records in the ascending order of the joining month of employees, so we have applied the ORDER BY clause. MONTH () function is applied on the column ‘Joining_Date’ to extract only the joining month from Joining_Date.

You will get the following output:

IDNameSalaryJoining_DateDOB
2Kunal Mohite340002021-01-011990-05-15 19:10:00
1Rohit More500002020-02-081991-01-28 18:06:08
3Saurabh Jha610002015-05-011983-02-20 12:18:45
6Bhavesh Jain370002021-07-031998-08-02 13:00:01
4Anant Desai590002018-08-271978-06-29 15:45:13
5Krishna Sharma480002010-10-231999-03-21 02:14:56

The results obtained from the above query show that the records are displayed according to the ascending order of the employee’s month of joining.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *