ORDER BY
ORDER BY in SQL is a clause used to sort the result set from a SQL query in either ascending (ASC) or descending (DESC) order according to one or more columns.
Example
Output
Explanation
The query selects all records from the Employees
table and orders them in ascending order (ASC
) by the LastName
column. Therefore, the result set is sorted from A to Z based on the employees’ last names. In case ASC
is not specified, the sorting default is ascending order.
Example
Output
Explanation
The Example provided is a SQL statement that retrieves employee names and their respective ages from the employees
table. It organizes the returned data in ascending order based on the employees’ ages. The ORDER BY
clause accomplishes this sorting.
Example
Output
Explanation
The SELECT * FROM Employees statement is used to select all records from the Employees table.
The ORDER BY LastName ASC clause sorts the result set by the LastName in ascending order. If LastName were the same for two employees, it uses the order they appear in the table.
Example
Output
Explanation
The SELECT statement orders the rows in ascending order (ASC
) by the Last_name
column. If two or more last names are the same, it orders these rows based on their positions in the database.
Example
Output
Explanation
The SQL ORDER BY
keyword sorts the result-set in ascending (default) or descending order. In this instance, the query is ordering the customers by their respective countries in ascending order.