SQL query Result ordering

Source: Internet
Author: User

Ascending sort:

When you use the ORDER BY clause, by default the data is sorted ascending, so you can use the ASC keyword pointing in ascending order, or unspecified, the default is ascending and the display is the same, as in the following figure:

Descending sort:

When you want query results to be sorted in descending order, you must specify the DESC keyword after sorting. The following figure is a descending arrangement to view employee salaries:

Common usage

To sort by using a non-select list:

Sorted columns can also not appear in the select list. The following figure:

To sort by using an expression:

Arithmetic expressions can appear in the columns of select, and expressions can be used to sort by expressions to evaluate the results. The following figure:

To sort using column aliases:

When you make a select query, you can define aliases for columns or expressions, and you can use aliases for sorting when you perform sorting. The following figure:

To sort by using column positions:

We can also sort by the position of the column or expression in the select list, which also effectively reduces the length of the sort statement. In addition, when you use a collection operator such as union, if the column name of the select list is different, you must sort by using the column position. The following figure: a standard that is sorted in ascending order with departmental codes and staff codes.

Use multiple column sorting:

When you perform an action, you can also sort by multiple columns. When more than one column is used as a sorting criterion, the first column is sorted first, if the first column data is the same, and then the second column is sorted, and so on. The following figure:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.