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:
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:
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
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:
Now, suppose you want to retrieve the employees sorted by their salary in descending order. You would use the ORDER BY clause like this:
The result of this query would be:
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.