We have discussed many situations in which programmer retrieve records based on a condition. The purpose of this clause in sql programming, is not to verify the result, but to sort the result set. Using this clause, programmer can sort the records either in ascending or descending order.
Programmer can use the ORDER BY clause in the SELECT statement to display the data in a specific order. The order may be ascending and descending, depend on the requirement of query result.
The Syntax of the ORDER BY clause:
SELECT select_list
FROM table_name
[ORDER BY order_by_expression [ASC|DESC]
[, order_by_expression [ASC|DESC]…]
Where
Optionally, you can also specify multiple columns, if you want to sort the result set based on more than one column. For this, you need to specify the sequence of the sort columns in the ORDER BY clause.
The following SQL query retrieves the record from the Department table by setting ascending order on the Name column:
SELECT DepartmentID, Name FROM HumanResources.Department ORDER BY Name ASC
Output: in the output the records are sorted alphabetically in ascending order according to name as shown in the image.
Now try the same query with DESC keyword.
SELECT DepartmentID, Name FROM HumanResources.Department ORDER BY Name DESC
Output: in the output the records are sorted alphabetically in descending order according to name as shown in the image.
Note: If you do not specify the ASC or DESC keywords with the column name in the ORDER BY clause, the records are sorted in the ascending order.
Programmer can use the ORDER BY clause in the SELECT statement to display the data in a specific order. The order may be ascending and descending, depend on the requirement of query result.
The Syntax of the ORDER BY clause:
SELECT select_list
FROM table_name
[ORDER BY order_by_expression [ASC|DESC]
[, order_by_expression [ASC|DESC]…]
Where
- Select_list: the list of field names to be displayed.
- Table_name: name of table from which records are to be retrieved.
- order_by_expression is the column name on which the sort is to be performed.
- ASC specifies that the values need to be sorted in ascending order.
- DESC specifies that the values need to be sorted in descending order.
Optionally, you can also specify multiple columns, if you want to sort the result set based on more than one column. For this, you need to specify the sequence of the sort columns in the ORDER BY clause.
The following SQL query retrieves the record from the Department table by setting ascending order on the Name column:
SELECT DepartmentID, Name FROM HumanResources.Department ORDER BY Name ASC
Output: in the output the records are sorted alphabetically in ascending order according to name as shown in the image.
Now try the same query with DESC keyword.
SELECT DepartmentID, Name FROM HumanResources.Department ORDER BY Name DESC
Output: in the output the records are sorted alphabetically in descending order according to name as shown in the image.
Note: If you do not specify the ASC or DESC keywords with the column name in the ORDER BY clause, the records are sorted in the ascending order.
Tidak ada komentar:
Posting Komentar