Get the App
SLTechnology News&Howtos  ›  Database  › 

SQL SERVER sorts the search results

Shulou Source: shulou.com Published: 2022-06-01 07:30:04 10月05日 Update

1. Sort by single column

Select column name from table name order by column name

2. Sort by multiple columns

Select column name 1, column name 2, column name 3 from table name order by column name 1, column name 2

Note: if there are multiple duplicates in column name 1, it will not be sorted by column name 2, but will be sorted by column name 2 only if column name 1 is unique.

3. Sort by column location

Select column name 1, column name 2, column name 3 from table name order by 2

Remarks: column name 1, column name 2, column name 3 are marked clearly in order to make the 2 and 3 after order by execute correctly.

4. Specify the sorting direction

The order by column name is followed by desc, which performs descending order.

The order by column name is not followed by desc, and the default is ascending order. If you perform ascending order, adding asc after order by does not affect it.

Select column name 1, column name 2, column name 3 from table name order by column name 1 desc

Specifies that a column is executed in descending order

Select column name 1, column name 2, column name 3 from table name order by column name 1 desc, column name 2, column name 3

Tags: Sort ascend comment clear position only direction tag order influence result retrieval Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn MySQL Shulou Technology Shulou Information