Order By Clause in SQL

Order By Clause in SQL is used to sort the resultset in ascending or descending order.

Key – Points :

  • By default it sorts the result in Ascending order.
  • Asc Keyword is used for Ascending Order and Desc Keyword is used for Descending  Order.
  • The Sorting can be done on the basis of one or more then one column.
  • Besides the Column names, Sorting can also be done on the basis of Column Position.
  • Order By always comes in the last of the Select sequence.

Syntax :

Sorting in Ascending Order –

SELECT <Col Name> from <Tablename> WHERE <Condition> ORDER BY <Col > Asc ;

SELECT <Col Name> from <Tablename> WHERE <Condition> ORDER BY <Col >;

Note : If no order is specified then by default it takes Ascending order.

Sorting in Descending Order –

SELECT <Col Name> from <Tablename> WHERE <Condition> ORDER BY <Col > DESC;

Sorting on the basis of multiple columns –

SELECT <Col Name> from <Tablename> WHERE <Condition> ORDER BY <Col 1> DESC , <Col 2 > ASC ;

Sorting on the bais of relative position –

SELECT <Col Name> from <Tablename> WHERE <Condition> ORDER BY <Col Position> DESC;

SELECT <Col Name> from <Tablename> WHERE <Condition> ORDER BY <Col Position> ASC;

 

Avatar photo

Shikha Katariya

Shikha

You may also like...