SQL ORDER BY Clause
SQL ORDER BY Clause
o Whenever we want to sort the records based on the columns stored in the tables
of the SQL database, then we consider using the ORDER BY clause in SQL.
o The ORDER BY clause in SQL will help us to sort the records based on the specific
column of a table. This means that all the values stored in the column on which
we are applying ORDER BY clause will be sorted, and the corresponding column
values will be displayed in the sequence in which we have obtained the values in
the earlier step.
o Using the ORDER BY clause, we can sort the records in ascending or descending
order as per our requirement. The records will be sorted in ascending order
whenever the ASC keyword is used with ORDER by clause. DESC keyword will
sort the records in descending order.
o If no keyword is specified after the column based on which we have to sort
the records, in that case, the sorting will be done by default in the ascending
order.
Before writing the queries for sorting the records, let us understand the syntax.
1. SELECT ColumnName1,...,ColumnNameN FROM TableName ORDER BY ColumnName
ASC;
1. SELECT ColumnName1,...,ColumnNameN FROM TableName ORDER BY ColumnName
DESC;
1. SELECT ColumnName1,...,ColumnNameN FROM TableName ORDER BY ColumnName;
Let us explore more on this topic with the help of examples. We will use the MySQL
database for writing the queries in examples.
Example 1:
Write a query to sort the records in the ascending order of the customer names
stored in the customers table.
Play Video
Query:
1. mysql> SELECT *FROM customers ORDER BY Name ASC;
Here in a SELECT query, an ORDER BY clause is applied on the column 'Name' to sort
the records. ASC keyword will sort the records in ascending order.
All the records present in the customers table are displayed in the ascending order of
the customer's name.
Example 2:
Write a query to sort the records in the ascending order of the addresses stored in
the customers table.
Query:
1. mysql> SELECT *FROM customers ORDER BY Address;
Here in a SELECT query, an ORDER BY clause is applied to the 'Address' column to sort
the records. No keyword is used after the ORDER BY clause. Hence, the records, by
default, will be sorted in ascending order.
All the records present in the customers table are displayed in the ascending order of
the customer's address.
Example 3:
Write a query to sort the records in the descending order of the customer salary
stored in the customers table.
Query:
1. mysql> SELECT *FROM customers ORDER BY Salary DESC;
Here in a SELECT query, an ORDER BY clause is applied on the column ?Salary? to sort
the records. DESC keyword will sort the records in descending order.
All the records present in the customers table are displayed in the descending order of
the customer's salary.
Example 4:
Write a query to sort the records in the descending order of the customer age
stored in the customers table.
Query:
1. mysql> SELECT *FROM customers ORDER BY Age DESC;
Here in a SELECT query, an ORDER BY clause is applied on the column 'Age' to sort the
records. DESC keyword will sort the records in descending order.
All the records present in the customers table are displayed in the descending order of
the customer's age.
Example 1:
Write a query to sort the records in the ascending order of the agent names stored
in the agents table.
Query:
1. mysql> SELECT *FROM agents ORDER BY Name ASC;
Here in a SELECT query, an ORDER BY clause is applied on the column 'Name' to sort
the records. ASC keyword will sort the records in ascending order.
All the records present in the agents table are displayed in the ascending order of the
agent's name.
Example 2:
Write a query to sort the records in the descending order of the work area stored
in the agents table.
Query:
1. mysql> SELECT *FROM agents ORDER BY WorkArea DESC;
All the records present in the agents table are displayed in the descending order of the
customer's work area.
Example 3:
Write a query to sort the records in the ascending order of the agent salary stored in the
agents table.
Query:
1. mysql> SELECT *FROM agents ORDER BY Salary;
Here in a SELECT query, an ORDER BY clause is applied on the column 'Salary' to sort the
records. No keyword is used after the ORDER BY clause. Hence, the records, by default,
will be sorted in ascending order.
All the records present in the agents table are displayed in the ascending order of the
customer's salary.
Example 4:
Write a query to sort the records in the descending order of the agent salary
stored in the agents table.
Query:
1. mysql> SELECT *FROM agents ORDER BY Salary DESC;
Here in a SELECT query, an ORDER BY clause is applied on the column 'Salary' to sort the
records. DESC keyword will sort the records in descending order.
All the records present in the agents table are displayed in the descending order of the
customer's address.
1. SELECT supplier_city
2. FROM suppliers
3. WHERE supplier_name = 'IBM'
4. ORDER BY supplier_city;
This is an example to sort the result in ascending order by NAME and SALARY.
Play Video
1. SELECT * FROM CUSTOMERS
2. ORDER BY NAME, SALARY;
1. SELECT supplier_city
2. FROM suppliers
3. WHERE supplier_name = 'IBM'
4. ORDER BY supplier_city DESC;
1. SELECT * FROM CUSTOMERS
2. ORDER BY NAME DESC;