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;

 

Print Friendly, PDF & Email
Logical Operators
Backup Table in SQL
Shikha Katariya

Shikha Katariya

Shikha Katariya ,the Blog author is QA Engineer by profession,Currently serving in MNC, She has more than 4 years of experience in software industry and has worked for domains like Insurance , Core & retail Banking. Always keen to learn new technologies , she has working experience in mainframes,informatica ,and ETL Testing.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *