---
title: "How does the ORDER BY clause work? Also, give an example."  
description: "How does the ORDER BY clause work? Also, give an example."  
author: "Steilla Mitchel"  
published: 2023-09-04  
updated: 2023-09-25  
canonical: https://www.mindstick.com/forum/159823/how-does-the-order-by-clause-work-also-give-an-example  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 3 minutes  

---

# How does the ORDER BY clause work? Also, give an example.

How does the [ORDER](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) BY [clause](https://www.mindstick.com/forum/33937/difference-between-throw-exception-and-throw-clause) work? Also, give an example.

## Replies

### Reply by Aryan Kumar

The **ORDER BY** clause in SQL is used to sort the result set of a query based on one or more columns. It allows you to specify the order in which the rows should appear in the query result. The default order is ascending (ASC), but you can also specify descending (DESC) order for any column.

Here's how the **ORDER BY** clause works:

Syntax:

```plaintext
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
```

**column1, column2, ...**: The columns you want to retrieve from the table.

**table_name**: The name of the table from which you're selecting data.

**condition** (optional): Any conditions you want to apply to filter the rows.

**ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...**: This clause specifies the columns by which you want to sort the result set. You can use one or more columns and specify the sorting order (ascending or descending) for each column. ASC is the default order, so it's optional.

**Example**:

Let's say you have a table called **Employees** with the following data:

| EmployeeID | FirstName | LastName | Salary |
| --- | --- | --- | --- |
| 1 | John | Smith | 60000 |
| 2 | Jane | Johnson | 55000 |
| 3 | Michael | Davis | 62000 |
| 4 | Sarah | Wilson | 58000 |
| 5 | David | Lee | 63000 |

Now, suppose you want to retrieve the employees sorted by their salary in descending order. You would use the **ORDER BY** clause like this:

```plaintext
SELECT EmployeeID, FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC;
```

The result of this query would be:

| EmployeeID | FirstName | LastName | Salary |
| --- | --- | --- | --- |
| 5 | David | Lee | 63000 |
| 3 | Michael | Davis | 62000 |
| 1 | John | Smith | 60000 |
| 4 | Sarah | Wilson | 58000 |
| 2 | Jane | Johnson | 55000 |

In this example, the **ORDER BY** clause sorted the rows based on the "Salary" column in descending order, so employees with higher salaries appear at the top of the result set.

You can use the **ORDER BY** clause to sort query results by one or more columns in ascending or descending order, making it a powerful tool for controlling the presentation of data in your SQL queries.


---

Original Source: https://www.mindstick.com/forum/159823/how-does-the-order-by-clause-work-also-give-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
