@@ -18,5 +18,126 @@ license: |
18
18
See the License for the specific language governing permissions and
19
19
limitations under the License.
20
20
---
21
+ The <code >ORDER BY</code > clause is used to return the result rows in a sorted manner
22
+ in the user specified order. Unlike the <code >SORT BY</code > clause, this clause guarantees
23
+ a total order in the output.
21
24
22
- ** This page is under construction**
25
+ ### Syntax
26
+ {% highlight sql %}
27
+ ORDER BY { expression [ sort_direction | nulls_sort_oder ] [ , ... ] }
28
+ {% endhighlight %}
29
+
30
+ ### Parameters
31
+ <dl >
32
+ <dt ><code ><em >ORDER BY</em ></code ></dt >
33
+ <dd >
34
+ Specifies a comma-separated list of expressions along with optional parameters <code>sort_direction</code>
35
+ and <code>nulls_sort_order</code> which are used to sort the rows.
36
+ </dd >
37
+ <dt ><code ><em >sort_direction</em ></code ></dt >
38
+ <dd >
39
+ Optionally specifies whether to sort the rows in ascending or descending
40
+ order. The valid values for the sort direction are <code>ASC</code> for ascending
41
+ and <code>DESC</code> for descending. If sort direction is not explicitly specified, then by default
42
+ rows are sorted ascending. <br><br>
43
+ <b>Syntax:</b>
44
+ <code>
45
+ [ ASC | DESC ]
46
+ </code>
47
+ </dd >
48
+ <dt ><code ><em >nulls_sort_order</em ></code ></dt >
49
+ <dd >
50
+ Optionally specifies whether NULL values are returned before/after non-NULL values, based on the
51
+ sort direction. In Spark, NULL values are considered to be lower than any non-NULL values by default.
52
+ Therefore the ordering of NULL values depend on the sort direction. If <code>null_sort_order</code> is
53
+ not specified, then NULLs sort first if sort order is <code>ASC</code> and NULLS sort last if
54
+ sort order is <code>DESC</code>.<br><br>
55
+ <ol>
56
+ <li> If <code>NULLS FIRST</code> (the default) is specified, then NULL values are returned first
57
+ regardless of the sort order.</li>
58
+ <li>If <code>NULLS LAST</code> is specified, then NULL values are returned last regardless of
59
+ the sort order. </li>
60
+ </ol><br>
61
+ <b>Syntax:</b>
62
+ <code>
63
+ [ NULLS { FIRST | LAST } ]
64
+ </code>
65
+ </dd >
66
+ </dl >
67
+
68
+ ### Examples
69
+ {% highlight sql %}
70
+ CREATE TABLE person (id INT, name STRING, age INT);
71
+ INSERT INTO person VALUES
72
+ (100, 'John', 30),
73
+ (200, 'Mary', NULL),
74
+ (300, 'Mike', 80),
75
+ (400, 'Jerry', NULL),
76
+ (500, 'Dan', 50);
77
+
78
+ -- Sort rows by age. By default rows are sorted in ascending manner.
79
+ SELECT name, age FROM person ORDER BY age;
80
+
81
+ +-----+----+
82
+ | name | age |
83
+ +-----+----+
84
+ | Jerry| null|
85
+ | Mary | null|
86
+ | John | 30 |
87
+ | Dan | 50 |
88
+ | Mike | 80 |
89
+ +-----+----+
90
+
91
+ -- Sort rows in ascending manner keeping null values to be last.
92
+ SELECT name, age FROM person ORDER BY age NULLS LAST;
93
+
94
+ +-----+----+
95
+ | name | age |
96
+ +-----+----+
97
+ | John | 30 |
98
+ | Dan | 50 |
99
+ | Mike | 80 |
100
+ | Mary | null|
101
+ | Jerry| null|
102
+ +-----+----+
103
+
104
+ -- Sort rows by age in descending manner.
105
+ SELECT name, age FROM person ORDER BY age DESC;
106
+
107
+ +-----+----+
108
+ | name | age |
109
+ +-----+----+
110
+ | Mike | 80 |
111
+ | Dan | 50 |
112
+ | John | 30 |
113
+ | Jerry| null|
114
+ | Mary | null|
115
+ +-----+----+
116
+
117
+ -- Sort rows in ascending manner keeping null values to be first.
118
+ SELECT name, age FROM person ORDER BY age DESC NULLS FIRST;
119
+
120
+ +-----+----+
121
+ | name | age |
122
+ +-----+----+
123
+ | Jerry| null|
124
+ | Mary | null|
125
+ | Mike | 80 |
126
+ | Dan | 50 |
127
+ | John | 30 |
128
+ +-----+----+
129
+
130
+ -- Sort rows based on more than one column with each column having different
131
+ -- sort direction.
132
+ SELECT * FROM person ORDER BY name ASC, age DESC;
133
+
134
+ +---+-----+----+
135
+ | id | name | age |
136
+ +---+-----+----+
137
+ | 500| Dan | 50 |
138
+ | 400| Jerry| null|
139
+ | 100| John | 30 |
140
+ | 200| Mary | null|
141
+ | 300| Mike | 80 |
142
+ +---+-----+----+
143
+ {% endhighlight %}
0 commit comments