Syntax for SQL ORDER BY Keyword.
SELECT column_name list
FROM table_name
ORDER BY column1, column2, ... ASC|DESC
Lets Learn.
1.Create database and table.
Student table.
StudentID | StudentName | Address | Country | Section | Branch | Percentage | CourseID |
---|---|---|---|---|---|---|---|
1 | Liam | 23 Andrew road | USA | A | ISE | 66 | 5 |
2 | Andrew | 2415 Anthony market str | Germany | A | ISE | 80 | 6 |
4 | Ryan | 18 Christopher sq. | INDIA | B | EE | 67 | 5 |
5 | Charles | 878 Eli Berguvsvägen 8 | UK | C | ECE | 52 | 8 |
for this above student table we use SQL ORDER BY keyword to order the records .
Example(1)
SELECT * FROM student ORDER BY Branch;
above SQL statement selects all student details from the "student" table and it will sort the "Branch" column by Ascending order.(default)
Example(2)
SELECT * FROM student ORDER BY Branch DESC;
above SQL statement selects all student from the "student " table and it will sort the " Branch " column by Descending order.
Example(3)
SELECT * FROM student
ORDER BY Branch ASC, StudentName DESC;
above SQL statement selects all student from the "student" table and it will sort branch column in Ascending order and StudentName in Descending order so we can ORDER BY Several columns.
2.Complete code of SQL ORDER BY Keyword in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
<title>How to use SQL ORDER BY Keyword</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="text-center">
<h1>SQL ORDER BY Keyword</h1>
</div>
<div class="well">
<?php
$connect = mysqli_connect("localhost", "root", "", "management");
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}
$sql_1 = "SELECT * FROM student ORDER BY Branch;";
$sql_2 = "SELECT * FROM student ORDER BY Branch DESC;";
$sql_3 = "SELECT * FROM student
ORDER BY Branch ASC, StudentName DESC;";
echo "<h4 class='text-danger'>Order By Ascending </h4>";
if($result_1 = mysqli_query($connect, $sql_1)){
if(mysqli_num_rows($result_1) > 0){
echo "<table class='table'>
<tr>
<th>StudentName</th>
<th>Branch</th>
</tr>
";
while($row = $result_1->fetch_assoc()){
echo "<tr>
<td>".$row['StudentName'] /*student table*/ ."</td>
<td>".$row['Branch'] ."</td>
</tr>";
}
echo "</table>";
} else{
echo "No records found.";
}
}
echo "<h4 class='text-danger'>Order By Descending </h4>";
if($result_2 = mysqli_query($connect, $sql_2)){
if(mysqli_num_rows($result_2) > 0){
echo "<table class='table'>
<tr>
<th>StudentName</th>
<th>Branch</th>
</tr>
";
while($row = $result_2->fetch_assoc()){
echo "<tr>
<td>".$row['StudentName'] /*student table*/ ."</td>
<td>".$row['Branch'] ."</td>
</tr>";
}
echo "</table>";
} else{
echo "No records found.";
}
}
echo "<h4 class='text-danger'>Example(3)</h4>";
if($result_3 = mysqli_query($connect, $sql_3)){
if(mysqli_num_rows($result_3) > 0){
echo "<table class='table'>
<tr>
<th>StudentName</th>
<th>Branch</th>
</tr>
";
while($row = $result_3->fetch_assoc()){
echo "<tr>
<td>".$row['StudentName'] /*student table*/ ."</td>
<td>".$row['Branch'] ."</td>
</tr>";
}
echo "</table>";
} else{
echo "No records found.";
}
}
?>
</div>
<br>
</div>
</body>
</html>