In SQL, what is the default sort order of the Order By clause?
By default, the order by statement will sort in ascending order if no order (whether ascending or descending) is explicitly specified. This means that because the default sort order is ascending, the values will be sorted starting from the “smallest” value to the largest. This is true in all major RDBMS’s – including MySQL, Oracle, Microsoft SQL Server, Teradata, SAP, and others.
An example showing the Order By default sort order:
Take a look at the simple table below.
Customers | ||||||||||
|
Now, let’s write some SQL to retrieve the cust_name values sorted by their respective cust_id’s, but note that we do not specify whether to sort by descending or ascending order:
select cust_name FROM Customers ORDER BY cust_id
Because the order by will work in ascending order by default, the SQL above will return the following results:
cust_name Sam Bill Joe Akash
Now you have seen the default behavior of the Order By clause in SQL – it will sort in ascending order.