Updated April 11, 2023
Introduction to 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.
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:
- column_name_1, column_name_2, …, column_name_n: columns or fields that have to be fetched for the final result set
- table_name: Database table from which the above-mentioned columns have to be fetched.
- condition_expression: Condition on the basis of which rows have to be filtered. This is optional.
- date_field: Column of date data type according to which the records must be sorted.
- ASC | DESC: Order of sorting as in ascending(ASC) or descending(DESC)
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:
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:
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:
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:
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:
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 #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:
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:
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:
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.