Sql-order by

Source: Internet
Author: User

It is often necessary to be able to make a systematic display of the captured data. This may be by small toward large (ascending) or by large toward small (descending). In this case, we can use order by as an order to achieve our purpose.

The syntax for ORDER by IS as follows:

 SELECT from [WHERE "condition"] ORDER by [ASC, DESC];

[] to represent WHERE is a must. However, if the where clause exists, it is before the ORDER by clause. The ASC represents the results in a small, large order, and DESC indicates that the results are listed in a small order. If neither is written, then we will use ASC.

We can arrange the order according to several different fields. In this case, the syntax for theORDER by clause is as follows (assuming there are two fields):

ORDER  by [ASC, DESC] [ASC, DESC]

If we choose between these two fields from small to large, then this clause will cause the result is based on "column one" from small to large rows. If there are several pieces of information "field one" value is equal, then these data will be based on "column two" from the small to large row.

SELECT from ORDER by DESC;

Results:

Los Angeles theJan- to-1999Boston theJan- ,-1999San Francisco -Jan- ,-1999San Diego -Jan- --1999

In the example above, we use the field names to specify the order of the columns. In addition to the column names, we can also use the order of the fields (according to the order in the SQL sentence). The first field after a SELECT is 1, the second field is 2, and so on. In the above example, we can achieve exactly the same result in SQL:

SELECT from ORDER by 2 DESC;

Sql-order by

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.