Using DESC to sort records in descending order Example #3įind order id, order amount, and ordered_at for all orders and sort results in descending order by ordered_at date. SELECT order_id, ordered_atįind the order id and shipping date of orders, arranged in ascending order by shipping date. Example #1įind the order id and ordering date and time of orders, arranged in ascending order by ordering time. Let us discuss examples of SQL ORDER BY DATE. Basic SQL Queries with DATE field values in ORDER BY clause Now we are all set to try a few examples based on the ORDER BY DATE clause with the help of this table. In the shipped_at field, we have just added a few days to the current date of the system. The former is of the DATETIME data type, and the latter is of the DATE data type. Note that we have two date fields, ordered_at and shipped_at columns. The data in the populated e-transactions table look something as follows: SELECT * FROM e_transactions We can use the following INSERT statement for this purpose. Let’s insert a few records in it to work with. We can use the following CREATE TABLE statement to create the said table. As the name suggests, the table contains details such as order date, ship date, etc., pertaining to orders placed on an e-commerce platform. 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”. Having learned the syntax and parameters used for writing ORDER BY clauses, let us try a few examples to understand the concept in detail. ASC | DESC: Order of sorting as in ascending(ASC) or descending(DESC).date_field: Column of date data type according to which the records must be sorted.condition_expression: Condition on the basis of which rows have to be filtered. table_name: Database table from which the above-mentioned columns have to be fetched.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |