SQL select query is used to fetch records from database. order by column name clause is used to sorting records in database table. Order by keyword is having options for sorting data records, one is ascending sorting order, Second is descending order, it will return records in higher to lower order. In ascending sorting order, it returns records in lower to higher order. order by clause can be implemented on all database Oracle, MS Sql (Microsoft SQL), MySQL.
SQL select order by query example explains how to use select order by query in SQL.
Table class_name with all records without desired column name sorting
+---------+-------------+------------+------------+ | Roll_No | Name | Class_Name | Year_Start | +---------+-------------+------------+------------+ | 1 | Amit Mishra | B Science | 2005 | | 2 | Robot | B.Tech | 2005 | | 3 | Rohan | M.Tech | 2005 | | 4 | Vivek | B.Tech | 2009 | | 5 | Chander | B.Science | 2009 | | 6 | Deepak | B.Science | 2009 | +---------+-------------+------------+------------+
Descending Order by Sorting
SELECT * FROM class_name c order by name desc;
Output
+---------+-------------+------------+------------+ | Roll_No | Name | Class_Name | Year_Start | +---------+-------------+------------+------------+ | 4 | Vivek | B.Tech | 2009 | | 3 | Rohan | M.Tech | 2005 | | 2 | Robot | B.Tech | 2005 | | 6 | Deepak | B.Science | 2009 | | 5 | Chander | B.Science | 2009 | | 1 | Amit Mishra | B Science | 2005 | +---------+-------------+------------+------------+
this SQL query in sql return rows from higher to lower sorting order from table.
SELECT * FROM class_name c order by name asc;
Output
+---------+-------------+------------+------------+ | Roll_No | Name | Class_Name | Year_Start | +---------+-------------+------------+------------+ | 1 | Amit Mishra | B Science | 2005 | | 5 | Chander | B.Science | 2009 | | 6 | Deepak | B.Science | 2009 | | 2 | Robot | B.Tech | 2005 | | 3 | Rohan | M.Tech | 2005 | | 4 | Vivek | B.Tech | 2009 | +---------+-------------+------------+------------+
this SQL query in sql return rows from lower to higher sorting order from table.
* indicates fetch all columns records from table.




Link to Us