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 have to specifically mention the DESC keyword if we want 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
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 have to 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 ecommerce platform. We can use the following CREATE TABLE statement to create the said table.
CREATE TABLE e_transactions
order_id character varying(255),
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
The data in the populated e-transactions table look something as follows:
SELECT * FROM e_transactions;
Note that we have two date fields, ordered_at, and shipped_at column. 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
ORDER BY ordered_at;
Example #2: Find the order id and shipping date of orders, arranged in ascending order by shipping date.
SELECT order_id, shipped_at
ORDER BY shipped_at;
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.
ORDER BY ordered_at DESC;
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.
ORDER BY ordered_at DESC, shipped_at ASC;
Using DATE PARTS such as MONTH, DAY, HOUR, etc. as an argument in 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.
ORDER BY DATEPART(Day,shipped_at);
Example #6: Find the order id, customer id, order amount, and hour of ordering for all orders, arranged in ascending order by an hour of ordering.
DATEPART(HOUR,ordered_at) as "Hour"
ORDER BY DATEPART(HOUR,ordered_at);
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 a month of ordering, and shipping date.
ORDER BY DATEPART(MONTH,ordered_at),shipped_at;
Using DATE obtained from DATEADD function as an argument in ORDER BY clause
Example #8: Find the order id, order amount and date and time of ordering, feedback date (this 15 days after the order has been shipped), arranged in ascending order by feedback date.
DATEADD(Day,15,shipped_at) As 'Feedback_date'
ORDER BY DATEADD(Day,15,shipped_at);
ORDER BY keyword clause is used to sort selected result sets into ascending or descending order according to one or more fields.
This is a guide to the SQL ORDER BY DATE. Here we discuss the Introduction, syntax, and examples with code implementation respectively. You may also have a look at the following articles to learn more –