Aaaaa
Aaaaa

Reputation: 163

ORDER BY clause in SQL

What is the meaning of ORDER BY 1,2

Can we use numeric values in the ORDER BY clause?

Is this a valid statement?

Upvotes: 1

Views: 1010

Answers (4)

Branimir
Branimir

Reputation: 4377

ORDER BY 1, 2 will order your data by first and second expressions in your select. For example:

SELECT 
    Col1,
    Col2
FROM 
    Table
ORDER BY 
    1

is the same as:

SELECT 
    Col1,
    Col2
FROM 
    Table
ORDER BY 
    Col1

You can use this notation with expressions, for example:

SELECT 
    10 - IntCol1,
    Col2
FROM 
    Table
ORDER BY 
    1

Upvotes: 3

Morten
Morten

Reputation: 3854

Numbers refer to the order of columns

Upvotes: 0

Jens
Jens

Reputation: 3299

This is a valid statement. The numbers point to the column position in your result.

ORDER BY 1, 2

is the same as

ORDER BY MyTable.FirstColumn, MyTable.SecondColumn

Upvotes: 3

Steven Ryssaert
Steven Ryssaert

Reputation: 1967

Yes, it is. It is ordering by columns without specifying the column name. So ordering by 1,2 is ordering by the first column, then by the second column.

Check out this article for more info:

http://www.1keydata.com/sql/sqlorderby.html

Upvotes: 1

Related Questions