SQL ORDER BY DATE

SQL ORDER BY DATE

ORDER BY DATE clause in structured query language (SQL) is used to arrange the result set fetched by a SELECT query in ascending or descending according to one or more DATE columns. It is similar to using the ORDER BY statement on any other string or integer-type column. By default, the statement sorts the result set in ascending order. We must specifically mention the DESC keyword to sort it in descending order.

ADVERTISEMENT Popular Course in this category SQL - Specialization | 51 Course Series | 6 Mock Tests

Hadoop, Data Science, Statistics & others

Let’s begin with understanding the syntax used for writing ORDER BY clauses.

Syntax and parameters:

The basic syntax used for writing the SELECT query with ORDER BY clause is as follows:

SELECT column_name_1, column_name_2, . column_name_n FROM table_name WHERE condition_expression ORDER BY date_field ASC | DESC;

The parameters used in the above-mentioned syntax are as follows:

Having learned the syntax and parameters used for writing ORDER BY clauses, let us try a few examples to understand the concept in detail.

Examples of SQL ORDER BY DATE

In order to illustrate the working of the ORDER BY DATE statement, what can be better than trying a few examples on a dummy table? Ergo, let’s create a dummy table called “e-transactions”. As the name suggests, the table contains details such as order date, ship date, etc., pertaining to orders placed on an e-commerce platform. We can use the following CREATE TABLE statement to create the said table.

CREATE TABLE e_transactions ( order_id character varying(255), ordered_at datetime, shipped_at date, order_amount numeric, customer_id character varying(255) );

The table has been successfully created. Let’s insert a few records in it to work with. We can use the following INSERT statement for this purpose.

INSERT INTO e_transactions (order_id ,ordered_at ,shipped_at ,order_amount ,customer_id) VALUES ('AJ202001','2020-07-02T10:48:00',DATEADD(DAY,5,GETDATE()),4315,'CA01'), ('AJ202004','2020-07-02T10:48:00',DATEADD(DAY,4,GETDATE()), 5460,'CA06'), ('AJ202003','2020-06-30T06:28:00',DATEADD(DAY,1,GETDATE()), 1462,'CA05'), ('AJ202005','2020-07-01T06:18:00',DATEADD(DAY,2,GETDATE()), 15646,'CA032'), ('AJ202002','2020-07-01T05:10:00',DATEADD(DAY,3,GETDATE()), 1978,'CA07'); GO

The data in the populated e-transactions table look something as follows:

SELECT * FROM e_transactions;

Output:

SQL ORDER BY DATE 1

Note that we have two date fields, ordered_at and shipped_at columns. The former is of the DATETIME data type, and the latter is of the DATE data type. In the shipped_at field, we have just added a few days to the current date of the system.

Now we are all set to try a few examples based on the ORDER BY DATE clause with the help of this table.

Basic SQL Queries with DATE field values in ORDER BY clause

Let us discuss examples of SQL ORDER BY DATE.

Example #1

Find the order id and ordering date and time of orders, arranged in ascending order by ordering time.

SELECT order_id, ordered_at FROM e_transactions ORDER BY ordered_at;

Output:

SQL ORDER BY DATE 2

Example #2

Find the order id and shipping date of orders, arranged in ascending order by shipping date.

SELECT order_id, shipped_at FROM e_transactions ORDER BY shipped_at;

Output:

SQL ORDER BY DATE 3

Using DESC to sort records in descending order

Example #3

Find order id, order amount, and ordered_at for all orders and sort results in descending order by ordered_at date.

SELECT order_id, order_amount, ordered_at FROM e_transactions ORDER BY ordered_at DESC;

Output:

SQL ORDER BY DATE 4

Example #4

Find order id, order amount, ordered_at, and shipped_at date for all orders and sort them in descending order by ordered_at field and, in case of similarity, by shipped_at field.

SELECT order_id, order_amount, ordered_at, shipped_at FROM e_transactions ORDER BY ordered_at DESC, shipped_at ASC;

Output:

SQL ORDER BY DATE 5

Using DATE PARTS such as MONTH, DAY, HOUR, etc., as an argument in the ORDER BY clause

Example #5

Find the order id, customer id, order amount, and shipping date of orders, arranged in ascending order by day of shipping.

SELECT order_id,customer_id,order_amount,shipped_at FROM e_transactions ORDER BY DATEPART(Day,shipped_at);

Output:

example 5

Example #6

Find the order id, customer id, order amount, and hour of ordering for all orders, arranged in ascending order by hour of ordering.

SELECT order_id, customer_id, order_amount, DATEPART(HOUR,ordered_at) as "Hour" FROM e_transactions ORDER BY DATEPART(HOUR,ordered_at);

Output:

example 6

Using two DATE arguments in ORDER BY clause

Example #7

Find the order id, customer id, order amount, ordering date and time, and shipping date for all orders, arranged in ascending order by the month of ordering and shipping date.

SELECT order_id, customer_id, order_amount, ordered_at, shipped_at FROM e_transactions ORDER BY DATEPART(MONTH,ordered_at),shipped_at;

Output:

example 7

Using DATE obtained from the DATEADD function as an argument in the ORDER BY clause

Example #8

Find the order id, order amount, date and time of ordering, and feedback date (this 15 days after the order has been shipped), arranged in ascending order by feedback date.

SELECT order_id, order_amount, ordered_at, DATEADD(Day,15,shipped_at) As 'Feedback_date' FROM e_transactions ORDER BY DATEADD(Day,15,shipped_at);

Output:

example 8

Conclusion

ORDER BY keyword clause is used to sort selected result sets into ascending or descending order according to one or more fields.

Recommended Articles

We hope that this EDUCBA information on “SQL ORDER BY DATE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL Table Partitioning
  2. SQLite exit
  3. SQL AFTER UPDATE Trigger
  4. SQL NOT Operator
ADVERTISEMENT MICROSOFT POWER BI - Specialization | 8 Course Series
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5 ADVERTISEMENT CYBER SECURITY & ETHICAL HACKING - Specialization | 13 Course Series | 3 Mock Tests
89 of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5 ADVERTISEMENT MICROSOFT AZURE - Specialization | 15 Course Series | 12 Mock Tests
73 of HD Videos
15 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5 ADVERTISEMENT KALI LINUX - Specialization | 6 Course Series
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5 Primary Sidebar

all.in.one: AI & DATA SCIENCE - 470+ Courses | 4655+ Hrs | 80+ Specializations | Tests | Certificates 4655+ Hours of HD Videos | 80+ Learning Paths | 470+ Courses | 50+ Projects | Verifiable Certificate of Completion | Lifetime Access

all.in.one: HUMAN RESOURCE - 40+ Courses | 247+ Hrs | 4+ Specializations | Tests | Certificates 247+ Hours of HD Videos | 4+ Learning Paths | 40+ Courses | Verifiable Certificate of Completion | Lifetime Access