SQL ORDER BY Keyword

The order by keyword is use to sort the records in ascending or descending order. We use ASC for ascending and DESC for sorting the records in descending order.

Basic Syntax

<?php  

    SELECT column1, column2, ...
    FROM table_name
    ORDER BY column1, column2, ... ASC|DESC;

?>

Example for ORDER BY

Note: where we didn’t mention the ASC or DESC it means it will sort the records in ascending order by default.

<?php
 
    SELECT * FROM Customers
    ORDER BY Country;

?>

Example for ORDER BY for Several Columns

We can sort several columns in different order. Like we sort a column in ascending order and the other in descending order,

<?php

    SELECT * FROM Customers
    ORDER BY Country ASC, CustomerName DESC;

?>