SQL ORDER BY Clause

The SQL ORDER BY clause is used for sorting data in ascending and descending order based on one or more columns of table of database

Some databases sort query results in ascending order by default.

SQL ORDER BY Clause

SQL ORDER BY syntax:

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

SQL ORDER BY ASC

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.

SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY;

SQL ORDER BY DESC

This statement is used to sort data in descending order. You should use the DESC attribute in your ORDER BY clause as follows.

SELECT * FROM CUSTOMERS
ORDER BY NAME DESC;

SQL ORDER BY RANDOM

If you want to return a random row with MY SQL, Use the following code:

SELECT column FROM table
ORDER BY RAND ()
LIMIT 1

SQL ORDER BY Multiple Columns

the following SQL statement selects all customers from the table named \”customer\”, stored by the \”country\” and \”Customer-Name\” columns:

SELECT * FROM customers
ORDER BY country, Customer-Name;

 

Scroll to Top