Database SQL statements Learn notes (3)-Sort Retrieve data

Source: Internet
Author: User

1. Sorting Data

If you do not sort, the data will generally appear in the order in which it appears in the underlying table, which may be the order in which the data was originally added to the table. However, if the data is subsequently updated or deleted, the order is affected by how the DBMS reuses the storage space.

Clauses: SQL statements consist of clauses, some are necessary, and others are optional. A clause is usually made up of keywords plus the data provided. For example, the FROM clause in the SELECT statement that we mentioned in our last note.

Ordering requires the ORDER BY clause

SELECT  from ORDER  by Prod_name;

Be sure to ensure that the ORDER BY clause is the last sentence

2. Sort by multiple columns

SELECTfrom ORDER by Prod_price,prod_name; 

Sort by Prod_price first, and then sort by prod_name

3. Sort By Column position

SELECT from ORDER by 2, 3;

2 refers to the second sort in the selected column first, and then the 3rd sort

4. Specify the sort direction

The order by default is in ascending order, and if descending, you need to specify the DESC keyword

SELECT from ORDER by DESC;

If you intend to sort by more than one column

SELECT from ORDER by DESC, Prod_name;

DESC is applied directly to the column in front of it, that is, prod_price, not to the Prod_name column, and if it is also followed by prod_name descending, add desc after it

The opposite is ASC, which specifies ascending

Database SQL statements Learn notes (3)-Sort Retrieve data

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.