SQL ORDER BY Clause

We have already seen SQL SELECT command to fetch data from MySQL table. When you select rows, the MySQL server is free to return them in any order, If you want result in sort form you can sort a result set by adding an ORDER BY clause that names the column or columns you want to sort by.
The SQL ORDER BY clause is used for sorting data in ascending and descending order based on one or more columns.
Some databases sort query results in ascending order by default.
You can sort result on more than one field.
You can use WHERE…LIKE clause in usual way to put condition.

SQL ORDER BY syntax:

SELECT expressions  FROM tables  WHERE conditions  ORDER BY expression [ASC | DESC];

SQL ORDER BY CLAUSE IN ASCENDING ORDER with PHP Script

This statement is used to sort data in ascending order. If you miss the ASC attribute, SQL ORDER BY query takes ascending order by default.
You can use syntax of ORDER BY clause into PHP function mysql_query().This function is used to execute SQL command and mysql_fetch_assoc() can be used to fetch all the selected data. This function returns row as an associative array, this function is faster then the mysql_fetch_array() function.

Let’s see the Example for mysql ORDER BY clause:

 <?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error());

$sql = mysql_query(“SELECT id, name, age, salary FROM emmployee ORDER BY  name ASC”);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>        
<?php
while($result = mysql_fetch_assoc($sql))
{
?>  
<tr>
<td><?php echo $result[‘id’];?></td>
<td><?php echo $result[‘name’];?></td>
<td><?php echo $result[‘age’];?></td>
<td><?php echo $result[‘salary’];?></td>
</tr>


<?php
} 
?>
<table>


Output will be look like that:

Id Name Age Salary
4 Nitesh Kumar 29 20000
2 Rahul Choudhary 22 25000
9 Roshan 26 23000

SQL ORDER BY CLAUSE IN DESCENDING ORDER with PHP Script

This statement is used to sort data in descending order. You should use the DESC attribute in your ORDER BY clause.
You can use syntax of ORDER BY clause into PHP function mysql_query().This function is used to execute SQL command and mysql_fetch_assoc() can be used to fetch all the selected data. This function returns row as an associative array, this function is faster then the mysql_fetch_array() function.

Let’s see the Example for mysql ORDER BY clause:

 <?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error());

$sql = mysql_query(“SELECT id, name, age, salary FROM emmployee ORDER BY  name AESC”);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>        
<?php
while($result = mysql_fetch_assoc($sql))
{
?>  
<tr>
<td><?php echo $result[‘id’];?></td>
<td><?php echo $result[‘name’];?></td>
<td><?php echo $result[‘age’];?></td>
<td><?php echo $result[‘salary’];?></td>
</tr>


<?php
} 
?>
<table>

Output will be look like that:

Id Name Age Salary
2 Rahul Choudhary 22 25000
4 Nitesh Kumar 29 20000
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.