Order by multi-field sorting query in SQL

Source: Internet
Author: User

In SQL, order by has N Different usage methods. Yesterday I talked about an order by with an equal sign. Here we will introduce how order by sorts multiple fields at the same time.


Two fields can be added after order by, which are separated BY commas.

Example

The Code is as follows: Copy code

Select a1, a2, a3 from t1 order by a1 desc, a2 asc

Next let's take a look at other instances.

F1 is in ascending order, f2 is in descending order, and SQL is written in this way

Order by f1, f2 DESC

You can also write it like this to make it clearer:

Order by f1 ASC, f2 DESC

If both are in descending order, you must use two desc

Order by f1 DESC, f2 DESC

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.