SQL Basic- SQL ORDER BY



SQL ORDER BY



This is Keyword, Used to sort the result-set.



SQL ORDER BY Keyword


The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. 

To sort the records in a ascending order, you can use the ASC keyword.

To sort the records in a descending order, you can use the DESC keyword.


Below is selection from "Student" table:






Syntax:


SELECT column_name
FROM table_name
ORDER BY column_name ASC | DESC;



Example 1:[In one column]




The following statement selects all student from the "Student" table, sorted by the "Address" column:




SELECT * FROM Student 

ORDER BY Address;



Result:






Example 2:[In two column]


The following statement selects all Student from the "Student" table, sorted DESCENDING by the "Address " column:

SELECT * FROM Student

ORDER BY Address DESC;




Result:









Example 3:[In two column]


The following statement selects all Student from the "Student" table, sorted by the "StudentName" and the "Address" column:


SELECT * FROM Student

ORDER BY StudentName, Address;



Result:



Example 4:[In two column]


The following statement selects all Student from the "Student" table, sorted ASC by the "StudentName" and DESC by the "Address" column:


SELECT * FROM Student

ORDER BY StudentName ASCAddress DESC;



Result: