The ORDER BY clause in SQL is used to sort the results of a SELECT statement. By default, the ORDER BY clause sorts the results in ascending order. To sort the results in descending order, you can use the DESC keyword.
For example, the following SELECT statement will return all of the rows from the
customers table, sorted by the customer_name column in ascending order:
SQL
SELECT * FROM customers ORDER BY customer_name;
To sort the results in descending order, you can use the following SELECT statement:
SQL
SELECT * FROM customers ORDER BY customer_name DESC;
You can also sort the results by multiple columns. To do this, you simply specify the names of the columns, separated by commas, in the ORDER BY clause. For example, the following SELECT statement will return all of the rows from the
customers table, sorted by the customer_name column in ascending order, and then by the
customer_id column in ascending order:
SQL
SELECT * FROM customers ORDER BY customer_name, customer_id;
You can also specify the direction of sorting for each column. To do this, you can use the ASC keyword for ascending order or the DESC keyword for descending order. For example, the following SELECT statement will return all of the rows from the
customers table, sorted by the customer_name column in descending order, and then by the
customer_id column in ascending order:
SQL
SELECT * FROM customers ORDER BY customer_name DESC, customer_id;
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 results of a SELECT statement. By default, the ORDER BY clause sorts the results in ascending order. To sort the results in descending order, you can use the DESC keyword.
For example, the following SELECT statement will return all of the rows from the
customerstable, sorted by thecustomer_namecolumn in ascending order:SQL
To sort the results in descending order, you can use the following SELECT statement:
SQL
You can also sort the results by multiple columns. To do this, you simply specify the names of the columns, separated by commas, in the ORDER BY clause. For example, the following SELECT statement will return all of the rows from the
customerstable, sorted by thecustomer_namecolumn in ascending order, and then by thecustomer_idcolumn in ascending order:SQL
You can also specify the direction of sorting for each column. To do this, you can use the ASC keyword for ascending order or the DESC keyword for descending order. For example, the following SELECT statement will return all of the rows from the
customerstable, sorted by thecustomer_namecolumn in descending order, and then by thecustomer_idcolumn in ascending order:SQL