How to use SQL ORDER BY Clause

In previous SQL post we had discussed about Data Definition Language  Statements such as  CREATE Command and ALTER Command and USE Command and DROP DATABASE Command and SQL SELECT Statement andSQL INSERT Statement and SQL UPDATE Statement and SQL WHERE Clause.

SQL ORDER BY Clause

The ORDER BY Clause is used to sort the records in ascending or descending order, based on one or more columns.
Mostly all database server query fetch records by default in ascending order.

The Syntax of ORDER BY Clause :

Example of how to use ORDER BY Clause

Here, we can understand ORDER BY Clase with sql table “StudentMst”

SELECT * FROM StudentMst

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
2 Rahul Surat 352200 7874444444
3 Jayesh Ahmedabad 352200 7874333333
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

 

SELECT * FROM StudentMst ORDER BY City

ID Name City Pincode Mobile
3 Jayesh Ahmedabad 352200 7874333333
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212
4 Dhvanish Bombay 380022 7874111111
1 Meera Bombay 380022 7874555555
2 Rahul Surat 352200 7874444444

 

SELECT * FROM StudentMst ORDER BY City DESC

ID Name City Pincode Mobile
2 Rahul Surat 352200 7874444444
4 Dhvanish Bombay 380022 7874111111
1 Meera Bombay 380022 7874555555
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212
3 Jayesh Ahmedabad 352200 7874333333

 

SELECT * FROM StudentMst ORDER BY Name ASC

ID Name City Pincode Mobile
4 Dhvanish Bombay 380022 7874111111
3 Jayesh Ahmedabad 352200 7874333333
1 Meera Bombay 380022 7874555555
2 Rahul Surat 352200 7874444444
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

Leave a Reply

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

7 + 9 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">