Skip to content

Commit d5b92b2

Browse files
dilipbiswalmaropu
authored andcommitted
[SPARK-30579][DOC] Document ORDER BY Clause of SELECT statement in SQL Reference
### What changes were proposed in this pull request? Document ORDER BY clause of SELECT statement in SQL Reference Guide. ### Why are the changes needed? Currently Spark lacks documentation on the supported SQL constructs causing confusion among users who sometimes have to look at the code to understand the usage. This is aimed at addressing this issue. ### Does this PR introduce any user-facing change? Yes. **Before:** There was no documentation for this. **After.** <img width="972" alt="Screen Shot 2020-01-19 at 11 50 57 PM" src="https://user-images.githubusercontent.com/14225158/72708034-ac0bdf80-3b16-11ea-81f3-48d8087e4e98.png"> <img width="972" alt="Screen Shot 2020-01-19 at 11 51 14 PM" src="https://user-images.githubusercontent.com/14225158/72708042-b0d09380-3b16-11ea-939e-905b8c031608.png"> <img width="972" alt="Screen Shot 2020-01-19 at 11 51 33 PM" src="https://user-images.githubusercontent.com/14225158/72708050-b4fcb100-3b16-11ea-95d2-e4e302cace1b.png"> ### How was this patch tested? Tested using jykyll build --serve Closes #27288 from dilipbiswal/sql-ref-select-orderby. Authored-by: Dilip Biswal <dkbiswal@gmail.com> Signed-off-by: Takeshi Yamamuro <yamamuro@apache.org>
1 parent 8629597 commit d5b92b2

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

docs/sql-ref-syntax-qry-select-orderby.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,126 @@ license: |
1818
See the License for the specific language governing permissions and
1919
limitations under the License.
2020
---
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.
2124

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

Comments
 (0)