PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • Use format
  • Use examples
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
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.
To find out all the SQL commands (functions, clauses, operators, ...) that can be used in a SQL query managed by HFSQL, see Commands that can be used in an SQL query managed by HFSQL.
Notes:
  • 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 must also be found in the SELECT clause (output items of query).
    Versions 22 and later
    The sort items specified in the ORDER BY clause may not be found in the SELECT clause (output items of query).
    New in version 22
    The sort items specified in the ORDER BY clause may not be found in the SELECT clause (output items of query).
    The sort items specified in the ORDER BY clause may not be found in the SELECT clause (output items of query).
Use format
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.
Note: The ORDER BY clause must always be found after the GROUP BY clause.
Use examples
  • 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

    Note: 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
Minimum required version
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment