0% found this document useful (0 votes)
6 views4 pages

(D) SQL ORDER BY Clause

The SQL ORDER BY clause is used to sort records in a table based on a specified column, allowing for ascending or descending order. The default sorting is ascending unless specified otherwise with the ASC or DESC keywords. Examples are provided for sorting customer names, addresses, salaries, and ages in both ascending and descending order.

Uploaded by

sharmasudip010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

(D) SQL ORDER BY Clause

The SQL ORDER BY clause is used to sort records in a table based on a specified column, allowing for ascending or descending order. The default sorting is ascending unless specified otherwise with the ASC or DESC keywords. Examples are provided for sorting customer names, addresses, salaries, and ages in both ascending and descending order.

Uploaded by

sharmasudip010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL ORDER BY Clause

o The ORDER BY clause in SQL will help us to sort the records based on the specific column
of a table.
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.

Syntax to sort the records in ascending order:

SELECT ColumnName1,...,ColumnNameN FROM TableName ORDER BY ColumnName ASC;

Syntax to sort the records in descending order:

SELECT ColumnName1,...,ColumnNameN FROM TableName ORDER BY ColumnNameDESC;

Syntax to sort the records in ascending order without using ASC


keyword:

SELECT ColumnName1,...,ColumnNameN FROM TableName ORDER BY ColumnName;


Example 1:

Write a query to sort the records in the ascending order of the customer
names stored in the customers table.

SELECT *FROM customers ORDER BY Name ASC;


Example 2:

Write a query to sort the records in the ascending order of the addresses
stored in the customers table.

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.

Example 3:

Write a query to sort the records in the descending order of the customer
salary stored in the customers table.

SELECT *FROM customers ORDER BY Salary DESC;


Example 4:

Write a query to sort the records in the descending order of the customer
age stored in the customers table.

SELECT *FROM customers ORDER BY Age DESC;

Use of both asc and dsc.

You might also like