7.5. Sorting Rows

7.5. Sorting Rows
7.5. Sorting Rows
After a query has produced an output table (after the select list has been processed) it can optionally  be sorted. If sorting is not chosen, the rows will be returned in an unspecified order. The actual order  in that case will depend on the scan and join plan types and the order on disk, but it must not be relied  on. A particular output ordering can only be guaranteed if the sort step is explicitly chosen.
After query execution, you can choose to sort the output table (after the implementation of the selection list). If you do not specify a sort, then return rows out of order. The actual return order on the order depends on the connection retrieved and implementation plan type and line disk, but this is an unreliable sort. If ordering is explicitly specified, then the output can be ensured in the order of the output is unique.
 
The ORDER BY clause specifies the sort order:
ORDER BY clause specifies the sort order:
 
SELECT select_list
FROM table_expression
ORDER BY sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }]
[, sort_expression2 [ASC | DESC] [NULLS { FIRST |
LAST }] ...]
 
The sort expression(s) can be any expression that would be valid in the query's select list. An example  is:
Sort expression can be applied to a query to select any expression in the list. E.g:
 
SELECT a, b FROM table1 ORDER BY a + b, c;
 
When more than one expression is specified, the later values are used to sort rows that are equal  according to the earlier values. Each expression can be followed by an optional ASC or DESC keyword  to set the sort direction to ascending or descending. ASC order is the default. Ascending order puts  smaller values first, where “smaller” is defined in terms of the < operator. Similarly, descending order  is determined with the > operator. 
If a plurality of expressions specified, according to the earlier value, and then sorts the rows of equal value later use. After each expression can be followed by an optional ASC or DESC keyword to set the sort direction to ascending or descending order. The default is ASC order. Ascending the smaller value the first place, where a "small" is defined according to <operator. Similarly, by the determined descending> operator.
 
The NULLS FIRST and NULLS LAST options can be used to determine whether nulls appear before  or after non-null values in the sort ordering. By default, null values sort as if larger than any non-null  value; that is, NULLS FIRST is the default for DESC order, and NULLS LAST otherwise.
NULLS FIRST and NULLS LAST option defines when or after sorting is null before the non-null value. By default, the null value is greater than the non-null value at the time of ordering, that is to say, in sort DESC, NULL FIRST is the default behavior, and NULL LAST is the default behavior of the ASC.
 
Note that the ordering options are considered independently for each sort column. For example ORDER  BY x, y DESC means ORDER BY x ASC, y DESC , which is not the same as ORDER BY  x DESC, y DESC .
Note that for each sort column, sorting options will be considered separately. For example, ORDER BY x, y DESC indicates ORDER BY x ASC, y DESC, and ORDER BY x DESC, y DESC different.
 
A sort_expression can also be the column label or number of an output column, as in:
sort_expression column labels or may be output column number, for example:
 
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
 
both of which sort by the first output column. Note that an output column name has to stand alone, that is, it cannot be used in an expression — for example, this is not correct:
According to the first embodiment are the output column. Note that the output column name must only output column name, that is to say, you can not use the expression - Example For example, the following is an error:
 
SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; --wrong
 
This restriction is made to reduce ambiguity. There is still ambiguity if an ORDER BY item is a simple  name that could match either an output column name or a column from the table expression. The  output column is used in such cases. This would only cause confusion if you use AS to rename an  output column to match some other table column's name.
This restriction is to prevent ambiguity. But for ORDER BY, remains ambiguous, such as the name may be referenced output column name may also be a column name table expression. In this case, use the output column name. This is only when using the AS rename the output column of the same name but do not care when the other table and column names, will create confusion.
 
ORDER BY can be applied to the result of a UNION , INTERSECT , or EXCEPT combination, but in  this case it is only permitted to sort by output column names or numbers, not by expressions.
The results can be applied to the ORDER BY UNION, INTERSECT or EXCEPT in, but then only can be sorted based on an output column name or number, the expression can not be used.
Published 341 original articles · won praise 54 · views 880 000 +

Guess you like

Origin blog.csdn.net/ghostliming/article/details/104551539