A concise tutorial of SQL statements for Linux---ORDER by

Source: Internet
Author: User

So far, we have learned how to capture data from a table with the two commands of SELECT and WHERE . However, we have not yet mentioned how this information should be arranged. This is actually a very important question. In fact, we often need 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 "Field name"
From "Table name"
[WHERE "Condition"]
ORDER by "field name" [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 "field One" [ASC, Desc], "column Two" [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.

For example, if we want to list the information in the store_information form in the Sales column from the big to the small,

store_information Form

Store_name Sales Txn_date
Los Angeles 1500 05-jan-1999
San Diego 250 07-jan-1999
San Francisco 300 08-jan-1999
Boston 700 08-jan-1999

We'll break in,

SELECT store_name, Sales, txn_date
From Store_information
ORDER by Sales DESC;

Results:

Store_name Sales Txn_date
Los Angeles 1500 jan-05-1999
Boston 700 jan-08-1999
San Francisco 300 jan-08-1999
San Diego 250 jan-07-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 store_name, Sales, txn_date
From Store_information
ORDER by 2 DESC;


Linux is measured as follows:





Reprint please specify: Xiao Liu

A concise tutorial of SQL statements for Linux---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.