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 | WHERE Clause
- SQL | USING Clause
- SQL | WITH clause
- SQL | Except Clause
- SQL | ON Clause
- Having clause in MS SQL Server
- SQL | LIMIT Clause
- Where clause in MS SQL Server
- SQL | Distinct Clause
- Difference between Where and Having Clause in SQL
- SQL | Union Clause
- SQL | SELECT TOP Clause
- SQL | With Ties Clause
- SQL | Intersect & Except clause
- Difference between From and Where Clause in SQL
- SQL | Sub queries in From 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 email@example.com. 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.