codescracker


sql

SQL Order By



« Previous Tutorial Next Tutorial »


SQL ORDER BY keyword is basically used to sort the result-set by one or more columns. Here is the general form of the ORDER BY keyword in SQL to sort the result-set by one/more columns:

SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Note - As the ORDER BY keyword in SQL sorts the records in ascending order. Therefore, if you want to sort the records in descending order, then use DESC keyword.

SQL ORDER BY Example

Here is an example of ORDER BY keyword in SQL. Let's consider the table named CUSTOMERS having the following records:

sql order by

Following is an example, which would sort the result in ascending order by NAME and SALARY:

SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY;

After querying the above SQL statements, you will get the following output:

sql order by keyword

Following is an example, which would sort the result in descending order by NAME:

SELECT * FROM CUSTOMERS
ORDER BY NAME DESC;

After querying the above SQL statements, you will get the following output:

sql order by desc

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test