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:

IdNameAgeSalary
4Nitesh Kumar2920000
2Rahul Choudhary2225000
9Roshan2623000

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:

IdNameAgeSalary
2Rahul Choudhary2225000
4Nitesh Kumar2920000

Leave a Reply

Related Posts: You may like

How to remove whitespace from string using php

Remove All Special Characters From String PHP

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.