AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL ORDER BY |
|
SQL > SQL Commands >
Order By
The ORDER BY command in SQL sorts the result set in either ascending or descending order. ORDER BY usually appears last in a SQL statement because it is performed after the result set has been retrieved. The ORDER BY clause sorts query results by one or more columns in ascending (ASC) or descending (DESC) order, and can even sort by expressions not present in the SELECT list.
SyntaxThe syntax for the ORDER BY statement is as follows:
The [ ] means that the WHERE statement is optional. However, if a WHERE clause exists, it comes before the ORDER BY clause. ASC means that the results will be shown in ascending order, and DESC means that the results will be shown in descending order. If neither is specified, the default is ASC. It is possible to order by more than one column. For the case where we sort by two columns, the ORDER BY clause above becomes
Assuming that we choose ascending order for both columns, the output will be ordered in ascending order according to column 1. If there is a tie for the value of column 1, we then sort in ascending order by column 2. There is no limit to the number of columns you can use in the ORDER BY clause. However the general rule of thumb is you should only include the columns that are necessary for your use case. ExamplesWe use the following table for Examples 1-3. Table Store_Information
Example 1: ORDER BY a single column using column nameTo list the contents of Table Store_Information by Sales in descending order, we key in,
Result:
Example 2: ORDER BY a single column using column positionIn addition to column name, we may also use column position (based on the SQL query) to indicate which column we want to apply the ORDER BY clause. The first column is 1, second column is 2, and so on. In the above example, we will achieve the same results by the following command:
Example 3: ORDER BY a single column using a column not in the SELECT statementThe column(s) we use to sort the result do not need to be in the SELECT clause. For example, the following SQL,
works fine and will give the following result:
Example 4: ORDER BY an expressionIt is also possible to sort the result by an expression. For example, in the following table, Table Product_Sales
we can use the SQL statement below to order the results by Revenue (defined as Price * Units):
Result:
Frequently Asked QuestionsWhat is the difference between ASC and DESC in ORDER BY?ASC (ascending) sorts values from lowest to highest — numerically for numbers and A–Z for text. DESC (descending) sorts from highest to lowest, or Z–A for text. If you omit both, SQL defaults to ASC. Can ORDER BY be used with WHERE and GROUP BY in the same query?Yes. The standard clause order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY. ORDER BY always comes last and is applied to the final result set after all filtering and grouping. Is it possible to ORDER BY a column alias?In most databases (MySQL, PostgreSQL, SQL Server) you can reference a column alias defined in the SELECT list within ORDER BY. For example: Does ORDER BY affect query performance?Sorting requires additional processing, especially on large result sets. Adding indexes on columns used in ORDER BY can significantly improve performance, as the database can leverage the index's sorted structure to avoid a full sort operation. ExercisesFor these exercises, assume we have a table called User_Sales with the following data: Table User_Sales
1. Which of the following SQL statement is valid? (There can be more than one answer)
2. What is the result of the following query?
3. What is the result of the following query?
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.