ORDER BY clause in SQL is used to sort the result set of a query in ascending or descending order based on one or more columns. It is often used to organize data in a meaningful way, such as sorting names alphabetically or numbers numerically.
Key Points
- Sorting Data: The
ORDER BYclause sorts the result set in ascending (ASC) or descending (DESC) order. - Multiple Columns: You can sort by multiple columns, with the primary sort column listed first.
- Default Order: If no order is specified, the default is ascending (
ASC). - The
ORDER BYclause is used with theSELECTstatement.
Syntax
column1, column2, ...: The columns you want to retrieve.table_name: The table from which to retrieve data.ASC: Sorts the result set in ascending order (default).DESC: Sorts the result set in descending order.
Examples
Suppose you have a table namedEmployees that stores employee details.
Table: Employees
| EmployeeID | Name | City | State | Salary |
|---|---|---|---|---|
| 1 | Anand | Chennai | Tamil Nadu | 50000 |
| 2 | Bala | Coimbatore | Tamil Nadu | 60000 |
| 3 | Kavitha | Karaikal | Puducherry | 55000 |
| 4 | Raj | Madurai | Tamil Nadu | 70000 |
| 5 | Kumar | Trichy | Tamil Nadu | 45000 |
| Name | City | Salary |
|---|---|---|
| Anand | Chennai | 50000 |
| Bala | Coimbatore | 60000 |
| Kavitha | Karaikal | 55000 |
| Kumar | Trichy | 45000 |
| Raj | Madurai | 70000 |
Sorting in Descending Order
To sort the employees by their salary in descending order:| Name | City | Salary |
|---|---|---|
| Raj | Madurai | 70000 |
| Bala | Coimbatore | 60000 |
| Kavitha | Karaikal | 55000 |
| Anand | Chennai | 50000 |
| Kumar | Trichy | 45000 |
Sorting by Multiple Columns
You can sort by multiple columns. For example, sort employees first by their state in ascending order and then by their salary in descending order.| Name | City | State | Salary |
|---|---|---|---|
| Kavitha | Karaikal | Puducherry | 55000 |
| Raj | Madurai | Tamil Nadu | 70000 |
| Bala | Coimbatore | Tamil Nadu | 60000 |
| Anand | Chennai | Tamil Nadu | 50000 |
| Kumar | Trichy | Tamil Nadu | 45000 |
Using ORDER BY with WHERE Clause
You can combine theORDER BY clause with the WHERE clause to filter and sort data. For example, retrieve employees from Tamil Nadu and sort them by salary in descending order.
| Name | City | Salary |
|---|---|---|
| Raj | Madurai | 70000 |
| Bala | Coimbatore | 60000 |
| Anand | Chennai | 50000 |
| Kumar | Trichy | 45000 |
Practical Use Case
Suppose you have a table namedStudents that stores student details, and you want to retrieve students sorted by their percentage in descending order.
Table: Students
| StudentID | Name | City | State | Percentage |
|---|---|---|---|---|
| 1 | Ram | Chennai | Tamil Nadu | 92 |
| 2 | Karthik | Coimbatore | Tamil Nadu | 88 |
| 3 | David | Bangalore | Karnataka | 95 |
| 4 | Kannan | Karaikal | Puducherry | 91 |
| 5 | Siva | Madurai | Tamil Nadu | 89 |
| Name | City | Percentage |
|---|---|---|
| David | Bangalore | 95 |
| Ram | Chennai | 92 |
| Kannan | Karaikal | 91 |
| Siva | Madurai | 89 |
| Karthik | Coimbatore | 88 |
Key Takeaways
- The
ORDER BYclause is used to sort the result set in ascending (ASC) or descending (DESC) order. - You can sort by one or more columns, with the primary sort column listed first.
- The default sorting order is ascending (
ASC). - The
ORDER BYclause can be combined with theWHEREclause to filter and sort data. - Sorting data makes it easier to analyze and interpret query results.