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

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.