The ORDER BY Clause can be used along with the SELECT statement to sort the data of specific fields in an ordered way. It is used to sort the result-set in ascending or descending order.
The basic syntax of the Order By clause is –
Implementation of the Order By Clause :
Let us consider the following table “Data” with three columns ‘FirstName’, ‘LastName’ and ‘Age’.
To sort the FirstName column in the Data table in ascending order, the following code can be used.
Order By Clause using Procedural Method :
Code Explanation :
- The “res” variable stores the data that is returned by the function mysql_query().
- Everytime mysqli_fetch_array() is invoked,it returns the next row from the res() set.
- The while loop is used to loop through all the rows of the table “data”.
Order By Clause using Object Oriented Method :
Order By Clause using PDO Method :
- PHP | MySQL WHERE Clause
- PHP | MySQL LIMIT Clause
- Difference between order by and group by clause in SQL
- Difference between Having clause and Group by clause
- SQL | ON Clause
- SQL | WITH clause
- SQL | USING Clause
- SQL | Except Clause
- SQL | WHERE Clause
- SQL | SELECT TOP Clause
- SQL | With Ties Clause
- SQL | Union Clause
- Where clause in MS SQL Server
- Difference between From and Where Clause in SQL
- SQL | Sub queries in From Clause
- Having clause in MS SQL Server
- SQL | LIMIT Clause
- Difference between Where and Having Clause in SQL
- SQL | Intersect & Except clause
- SQL | Distinct Clause
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.