Skip to content
Related Articles

Related Articles

Improve Article
SQL | ORDER BY
  • Difficulty Level : Easy
  • Last Updated : 09 Jan, 2019

The ORDER BY statement in sql is used to sort the fetched data in either ascending or descending according to one or more columns.

  • By default ORDER BY sorts the data in ascending order.
  • We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

Syntax of all ways of using ORDER BY is shown below:

  • Sort according to one column: To sort in ascending or descending order we can use the keywords ASC or DESC respectively.
    Syntax:
    SELECT * FROM table_name ORDER BY column_name ASC|DESC
    
    table_name: name of the table.
    column_name: name of the column according to which the data is needed to be arranged.
    ASC: to sort the data in ascending order.
    DESC: to sort the data in descending order.
    | : use either ASC or DESC to sort in ascending or descending order
    
  • Sort according to multiple columns: To sort in ascending or descending order we can use the keywords ASC or DESC respectively. To sort according to multiple columns, separate the names of columns by (,) operator.
    Syntax:
    SELECT * FROM table_name ORDER BY column1 ASC|DESC , column2 ASC|DESC
    

 



Screenshot from 2016-12-19 12-53-29

Queries:

  • Sort according to single column: In this example we will fetch all data from the table Student and sort the result in descending order according to the column ROLL_NO.
    SELECT * FROM Student ORDER BY ROLL_NO DESC;
    

    Output:

    ROLL_NONAMEADDRESSPHONEAge
    8NIRAJALIPURXXXXXXXXXX19
    7ROHITBALURGHATXXXXXXXXXX18
    6DHANRAJBARABAJARXXXXXXXXXX20
    5SAPTARHIKOLKATAXXXXXXXXXX19
    4DEEPRAMNAGARXXXXXXXXXX18
    3RIYANKASILIGURIXXXXXXXXXX20
    2PRATIKBIHARXXXXXXXXXX19
    1HARSHDELHIXXXXXXXXXX18

    To sort in ascending order we have to use ASC in place of DESC.

  • Sort according to multiple columns:In this example we will fetch all data from the table Student and then sort the result in ascending order first according to the column Age. and then in descending order according to the column ROLL_NO.
    SELECT * FROM Student ORDER BY Age ASC , ROLL_NO DESC;
    

    Output:

    ROLL_NONAMEADDRESSPHONEAge
    7ROHITBALURGHATXXXXXXXXXX18
    4DEEPRAMNAGARXXXXXXXXXX18
    1HARSHDELHIXXXXXXXXXX18
    8NIRAJALIPURXXXXXXXXXX19
    5SAPTARHIKOLKATAXXXXXXXXXX19
    2PRATIKBIHARXXXXXXXXXX19
    6DHANRAJBARABAJARXXXXXXXXXX20
    3RIYANKASILIGURIXXXXXXXXXX20

    In the above output you can see that first the result is sorted in ascending order according to Age.

    There are multiple rows having same Age. Now, sorting further this result-set according to ROLL_NO will sort the rows with same Age according to ROLL_NO in descending order.

  • Note that: ASC is the default value for ORDER BY clause. So, if you don’t specify anything after column name in ORDER BY clause, the output will be sorted in ascending order by default.

    Example: The following query will give similar output as the above:

    SELECT * FROM Student ORDER BY Age , ROLL_NO DESC;
    

    Output:

    ROLL_NONAMEADDRESSPHONEAge
    7ROHITBALURGHATXXXXXXXXXX18
    4DEEPRAMNAGARXXXXXXXXXX18
    1HARSHDELHIXXXXXXXXXX18
    8NIRAJALIPURXXXXXXXXXX19
    5SAPTARHIKOLKATAXXXXXXXXXX19
    2PRATIKBIHARXXXXXXXXXX19
    6DHANRAJBARABAJARXXXXXXXXXX20
    3RIYANKASILIGURIXXXXXXXXXX20

This article is contributed by Harsh Agarwal If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :