⚡ 180+ new openings posted today

SQL ORDER BY Clause

The ORDER BY Clause in SQL is used with the select statement to sort the data in ascending order or descending order, based on one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Syntax

SELECT column1, column2
FROM table_name
ORDER BY column_name ASC;
  • ASC → Ascending order (smallest to largest, A to Z)
  • DESC → Descending order (largest to smallest, Z to A)
  • ASC is the default if no direction is specified.

Example 1: Sort in Ascending Order

Suppose we have an Employees table:

IDNameSalary
1Mayank60000
2Ayush45000
3Sakshi75000
4Shivansh50000
SELECT *
FROM Employees
ORDER BY Salary ASC;

Result:

IDNameSalary
2Ayush45000
4Shivnash50000
1Mayank60000
3Sakshi75000

Example 2: Sort in Descending Order

SELECT *
FROM Employees
ORDER BY Salary DESC;

Result:

IDNameSalary
3Sakshi75000
1Mayank60000
4Shivansh50000
2Ayush45000

Example 3: Sort by Name

SELECT *
FROM Employees
ORDER BY Name ASC;

This sorts employee names alphabetically from A to Z.

ORDER BY with WHERE

ORDER BY can be used along with WHERE:

SELECT *
FROM Employees
WHERE Salary > 40000
ORDER BY Salary DESC;

This first selects employees earning more than 40,000 and then displays them from highest salary to lowest salary.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top