- Overview
- Use format
- Usage example
SQL : ORDER BY clause: Organizing the result of a query
The ORDER BY clause is used to sort the records selected by an SQL query. The selected records can be sorted in: - ascending order (default order):
- alphabetical order from A to Z for the text items.
- from the lowest value to the greatest value for the numeric items.
- descending order:
- alphabetical order from Z to A for the text items.
- from the greatest value to the lowest value for the numeric items.
See a specific documentation about SQL for more details. Remarks: - This clause can only be used:
- in the SQL code of queries created in the query editor. Then, these queries will be run by HExecuteQuery.
- in the SQL code of queries run by HExecuteSQLQuery.
- This clause can be used with all the types of data sources (Oracle, Access, SQL Server, ...).
- The sort items specified in the ORDER BY clause may not be found in the SELECT clause (output items of query).
The format for using the ORDER BY clause is as follows:
ORDER BY ItemToSort ASC/DESC
The <ItemToSort> parameter can correspond to: - the name of the item to sort.
- a parameter name.
When the query is run, if the parameter contains the sort order (ASC/DESC), this one will have priority over the one specified in the query.
Remark: The ORDER BY clause must always be found after the GROUP BY clause. - The following SQL code is used to list the last and first names of customers. The result is sorted by customer names in ascending order:
SELECT CUSTOMER.CustomerLastName, CUSTOMER.CustomerFirstName
FROM CUSTOMER
ORDER BY CUSTOMER.CustomerName
Remark: By default, the sort is performed in ascending order. Therefore, there is no need to specify the ASC keyword.
- The following SQL code is used to list the products. The result is sorted by product prices in descending order:
SELECT PRODUCT.ProductName, PRODUCT.ProductPrice
FROM PRODUCT
ORDER BY PRODUCT.ProductPrice DESC
This page is also available for…
|
|
|
|