2019-08-19 02:17:50 -04:00
|
|
|
---
|
|
|
|
layout: global
|
|
|
|
title: ORDER BY Clause
|
|
|
|
displayTitle: ORDER BY Clause
|
|
|
|
license: |
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
2020-03-11 19:52:40 -04:00
|
|
|
|
2019-08-19 02:17:50 -04:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2020-03-11 19:52:40 -04:00
|
|
|
|
2019-08-19 02:17:50 -04:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
---
|
[SPARK-31383][SQL][DOC] Clean up the SQL documents in docs/sql-ref*
### What changes were proposed in this pull request?
This PR intends to clean up the SQL documents in `doc/sql-ref*`.
Main changes are as follows;
- Fixes wrong syntaxes and capitalize sub-titles
- Adds some DDL queries in `Examples` so that users can run examples there
- Makes query output in `Examples` follows the `Dataset.showString` (right-aligned) format
- Adds/Removes spaces, Indents, or blank lines to follow the format below;
```
---
license...
---
### Description
Writes what's the syntax is.
### Syntax
{% highlight sql %}
SELECT...
WHERE... // 4 indents after the second line
...
{% endhighlight %}
### Parameters
<dl>
<dt><code><em>Param Name</em></code></dt>
<dd>
Param Description
</dd>
...
</dl>
### Examples
{% highlight sql %}
-- It is better that users are able to execute example queries here.
-- So, we prepare test data in the first section if possible.
CREATE TABLE t (key STRING, value DOUBLE);
INSERT INTO t VALUES
('a', 1.0), ('a', 2.0), ('b', 3.0), ('c', 4.0);
-- query output has 2 indents and it follows the `Dataset.showString`
-- format (right-aligned).
SELECT * FROM t;
+---+-----+
|key|value|
+---+-----+
| a| 1.0|
| a| 2.0|
| b| 3.0|
| c| 4.0|
+---+-----+
-- Query statements after the second line have 4 indents.
SELECT key, SUM(value)
FROM t
GROUP BY key;
+---+----------+
|key|sum(value)|
+---+----------+
| c| 4.0|
| b| 3.0|
| a| 3.0|
+---+----------+
...
{% endhighlight %}
### Related Statements
* [XXX](xxx.html)
* ...
```
### Why are the changes needed?
The most changes of this PR are pretty minor, but I think the consistent formats/rules to write documents are important for long-term maintenance in our community
### Does this PR introduce any user-facing change?
Yes.
### How was this patch tested?
Manually checked.
Closes #28151 from maropu/MakeRightAligned.
Authored-by: Takeshi Yamamuro <yamamuro@apache.org>
Signed-off-by: Sean Owen <srowen@gmail.com>
2020-04-13 00:40:36 -04:00
|
|
|
|
|
|
|
### Description
|
|
|
|
|
2020-05-10 13:57:25 -04:00
|
|
|
The `ORDER BY` clause is used to return the result rows in a sorted manner
|
2020-01-29 09:41:40 -05:00
|
|
|
in the user specified order. Unlike the [SORT BY](sql-ref-syntax-qry-select-sortby.html)
|
2020-03-11 19:52:40 -04:00
|
|
|
clause, this clause guarantees a total order in the output.
|
2019-08-19 02:17:50 -04:00
|
|
|
|
2020-01-25 19:11:33 -05:00
|
|
|
### Syntax
|
[SPARK-31383][SQL][DOC] Clean up the SQL documents in docs/sql-ref*
### What changes were proposed in this pull request?
This PR intends to clean up the SQL documents in `doc/sql-ref*`.
Main changes are as follows;
- Fixes wrong syntaxes and capitalize sub-titles
- Adds some DDL queries in `Examples` so that users can run examples there
- Makes query output in `Examples` follows the `Dataset.showString` (right-aligned) format
- Adds/Removes spaces, Indents, or blank lines to follow the format below;
```
---
license...
---
### Description
Writes what's the syntax is.
### Syntax
{% highlight sql %}
SELECT...
WHERE... // 4 indents after the second line
...
{% endhighlight %}
### Parameters
<dl>
<dt><code><em>Param Name</em></code></dt>
<dd>
Param Description
</dd>
...
</dl>
### Examples
{% highlight sql %}
-- It is better that users are able to execute example queries here.
-- So, we prepare test data in the first section if possible.
CREATE TABLE t (key STRING, value DOUBLE);
INSERT INTO t VALUES
('a', 1.0), ('a', 2.0), ('b', 3.0), ('c', 4.0);
-- query output has 2 indents and it follows the `Dataset.showString`
-- format (right-aligned).
SELECT * FROM t;
+---+-----+
|key|value|
+---+-----+
| a| 1.0|
| a| 2.0|
| b| 3.0|
| c| 4.0|
+---+-----+
-- Query statements after the second line have 4 indents.
SELECT key, SUM(value)
FROM t
GROUP BY key;
+---+----------+
|key|sum(value)|
+---+----------+
| c| 4.0|
| b| 3.0|
| a| 3.0|
+---+----------+
...
{% endhighlight %}
### Related Statements
* [XXX](xxx.html)
* ...
```
### Why are the changes needed?
The most changes of this PR are pretty minor, but I think the consistent formats/rules to write documents are important for long-term maintenance in our community
### Does this PR introduce any user-facing change?
Yes.
### How was this patch tested?
Manually checked.
Closes #28151 from maropu/MakeRightAligned.
Authored-by: Takeshi Yamamuro <yamamuro@apache.org>
Signed-off-by: Sean Owen <srowen@gmail.com>
2020-04-13 00:40:36 -04:00
|
|
|
|
2020-05-10 13:57:25 -04:00
|
|
|
```sql
|
2020-11-29 23:59:51 -05:00
|
|
|
ORDER BY { expression [ sort_direction | nulls_sort_order ] [ , ... ] }
|
2020-05-10 13:57:25 -04:00
|
|
|
```
|
2020-01-25 19:11:33 -05:00
|
|
|
|
|
|
|
### Parameters
|
[SPARK-31383][SQL][DOC] Clean up the SQL documents in docs/sql-ref*
### What changes were proposed in this pull request?
This PR intends to clean up the SQL documents in `doc/sql-ref*`.
Main changes are as follows;
- Fixes wrong syntaxes and capitalize sub-titles
- Adds some DDL queries in `Examples` so that users can run examples there
- Makes query output in `Examples` follows the `Dataset.showString` (right-aligned) format
- Adds/Removes spaces, Indents, or blank lines to follow the format below;
```
---
license...
---
### Description
Writes what's the syntax is.
### Syntax
{% highlight sql %}
SELECT...
WHERE... // 4 indents after the second line
...
{% endhighlight %}
### Parameters
<dl>
<dt><code><em>Param Name</em></code></dt>
<dd>
Param Description
</dd>
...
</dl>
### Examples
{% highlight sql %}
-- It is better that users are able to execute example queries here.
-- So, we prepare test data in the first section if possible.
CREATE TABLE t (key STRING, value DOUBLE);
INSERT INTO t VALUES
('a', 1.0), ('a', 2.0), ('b', 3.0), ('c', 4.0);
-- query output has 2 indents and it follows the `Dataset.showString`
-- format (right-aligned).
SELECT * FROM t;
+---+-----+
|key|value|
+---+-----+
| a| 1.0|
| a| 2.0|
| b| 3.0|
| c| 4.0|
+---+-----+
-- Query statements after the second line have 4 indents.
SELECT key, SUM(value)
FROM t
GROUP BY key;
+---+----------+
|key|sum(value)|
+---+----------+
| c| 4.0|
| b| 3.0|
| a| 3.0|
+---+----------+
...
{% endhighlight %}
### Related Statements
* [XXX](xxx.html)
* ...
```
### Why are the changes needed?
The most changes of this PR are pretty minor, but I think the consistent formats/rules to write documents are important for long-term maintenance in our community
### Does this PR introduce any user-facing change?
Yes.
### How was this patch tested?
Manually checked.
Closes #28151 from maropu/MakeRightAligned.
Authored-by: Takeshi Yamamuro <yamamuro@apache.org>
Signed-off-by: Sean Owen <srowen@gmail.com>
2020-04-13 00:40:36 -04:00
|
|
|
|
2020-05-10 13:57:25 -04:00
|
|
|
* **ORDER BY**
|
|
|
|
|
|
|
|
Specifies a comma-separated list of expressions along with optional parameters `sort_direction`
|
|
|
|
and `nulls_sort_order` which are used to sort the rows.
|
|
|
|
|
|
|
|
* **sort_direction**
|
|
|
|
|
2020-01-25 19:11:33 -05:00
|
|
|
Optionally specifies whether to sort the rows in ascending or descending
|
2020-05-10 13:57:25 -04:00
|
|
|
order. The valid values for the sort direction are `ASC` for ascending
|
|
|
|
and `DESC` for descending. If sort direction is not explicitly specified, then by default
|
|
|
|
rows are sorted ascending.
|
|
|
|
|
|
|
|
**Syntax:** [ ASC `|` DESC ]
|
|
|
|
|
|
|
|
* **nulls_sort_order**
|
|
|
|
|
2020-03-11 19:52:40 -04:00
|
|
|
Optionally specifies whether NULL values are returned before/after non-NULL values. If
|
2020-05-10 13:57:25 -04:00
|
|
|
`null_sort_order` is not specified, then NULLs sort first if sort order is
|
|
|
|
`ASC` and NULLS sort last if sort order is `DESC`.
|
|
|
|
|
|
|
|
1. If `NULLS FIRST` is specified, then NULL values are returned first
|
|
|
|
regardless of the sort order.
|
|
|
|
2. If `NULLS LAST` is specified, then NULL values are returned last regardless of
|
|
|
|
the sort order.
|
|
|
|
|
|
|
|
**Syntax:** `[ NULLS { FIRST | LAST } ]`
|
2020-01-25 19:11:33 -05:00
|
|
|
|
|
|
|
### Examples
|
[SPARK-31383][SQL][DOC] Clean up the SQL documents in docs/sql-ref*
### What changes were proposed in this pull request?
This PR intends to clean up the SQL documents in `doc/sql-ref*`.
Main changes are as follows;
- Fixes wrong syntaxes and capitalize sub-titles
- Adds some DDL queries in `Examples` so that users can run examples there
- Makes query output in `Examples` follows the `Dataset.showString` (right-aligned) format
- Adds/Removes spaces, Indents, or blank lines to follow the format below;
```
---
license...
---
### Description
Writes what's the syntax is.
### Syntax
{% highlight sql %}
SELECT...
WHERE... // 4 indents after the second line
...
{% endhighlight %}
### Parameters
<dl>
<dt><code><em>Param Name</em></code></dt>
<dd>
Param Description
</dd>
...
</dl>
### Examples
{% highlight sql %}
-- It is better that users are able to execute example queries here.
-- So, we prepare test data in the first section if possible.
CREATE TABLE t (key STRING, value DOUBLE);
INSERT INTO t VALUES
('a', 1.0), ('a', 2.0), ('b', 3.0), ('c', 4.0);
-- query output has 2 indents and it follows the `Dataset.showString`
-- format (right-aligned).
SELECT * FROM t;
+---+-----+
|key|value|
+---+-----+
| a| 1.0|
| a| 2.0|
| b| 3.0|
| c| 4.0|
+---+-----+
-- Query statements after the second line have 4 indents.
SELECT key, SUM(value)
FROM t
GROUP BY key;
+---+----------+
|key|sum(value)|
+---+----------+
| c| 4.0|
| b| 3.0|
| a| 3.0|
+---+----------+
...
{% endhighlight %}
### Related Statements
* [XXX](xxx.html)
* ...
```
### Why are the changes needed?
The most changes of this PR are pretty minor, but I think the consistent formats/rules to write documents are important for long-term maintenance in our community
### Does this PR introduce any user-facing change?
Yes.
### How was this patch tested?
Manually checked.
Closes #28151 from maropu/MakeRightAligned.
Authored-by: Takeshi Yamamuro <yamamuro@apache.org>
Signed-off-by: Sean Owen <srowen@gmail.com>
2020-04-13 00:40:36 -04:00
|
|
|
|
2020-05-10 13:57:25 -04:00
|
|
|
```sql
|
2020-01-25 19:11:33 -05:00
|
|
|
CREATE TABLE person (id INT, name STRING, age INT);
|
|
|
|
INSERT INTO person VALUES
|
|
|
|
(100, 'John', 30),
|
|
|
|
(200, 'Mary', NULL),
|
|
|
|
(300, 'Mike', 80),
|
|
|
|
(400, 'Jerry', NULL),
|
|
|
|
(500, 'Dan', 50);
|
|
|
|
|
2020-03-11 19:52:40 -04:00
|
|
|
-- Sort rows by age. By default rows are sorted in ascending manner with NULL FIRST.
|
2020-01-25 19:11:33 -05:00
|
|
|
SELECT name, age FROM person ORDER BY age;
|
2020-05-01 13:11:43 -04:00
|
|
|
+-----+----+
|
|
|
|
| name| age|
|
|
|
|
+-----+----+
|
|
|
|
|Jerry|null|
|
|
|
|
| Mary|null|
|
|
|
|
| John| 30|
|
|
|
|
| Dan| 50|
|
|
|
|
| Mike| 80|
|
|
|
|
+-----+----+
|
2020-01-25 19:11:33 -05:00
|
|
|
|
|
|
|
-- Sort rows in ascending manner keeping null values to be last.
|
|
|
|
SELECT name, age FROM person ORDER BY age NULLS LAST;
|
2020-05-01 13:11:43 -04:00
|
|
|
+-----+----+
|
|
|
|
| name| age|
|
|
|
|
+-----+----+
|
|
|
|
| John| 30|
|
|
|
|
| Dan| 50|
|
|
|
|
| Mike| 80|
|
|
|
|
| Mary|null|
|
|
|
|
|Jerry|null|
|
|
|
|
+-----+----+
|
2020-01-25 19:11:33 -05:00
|
|
|
|
2020-03-11 19:52:40 -04:00
|
|
|
-- Sort rows by age in descending manner, which defaults to NULL LAST.
|
2020-01-25 19:11:33 -05:00
|
|
|
SELECT name, age FROM person ORDER BY age DESC;
|
2020-05-01 13:11:43 -04:00
|
|
|
+-----+----+
|
|
|
|
| name| age|
|
|
|
|
+-----+----+
|
|
|
|
| Mike| 80|
|
|
|
|
| Dan| 50|
|
|
|
|
| John| 30|
|
|
|
|
|Jerry|null|
|
|
|
|
| Mary|null|
|
|
|
|
+-----+----+
|
2020-01-25 19:11:33 -05:00
|
|
|
|
|
|
|
-- Sort rows in ascending manner keeping null values to be first.
|
|
|
|
SELECT name, age FROM person ORDER BY age DESC NULLS FIRST;
|
2020-05-01 13:11:43 -04:00
|
|
|
+-----+----+
|
|
|
|
| name| age|
|
|
|
|
+-----+----+
|
|
|
|
|Jerry|null|
|
|
|
|
| Mary|null|
|
|
|
|
| Mike| 80|
|
|
|
|
| Dan| 50|
|
|
|
|
| John| 30|
|
|
|
|
+-----+----+
|
2020-01-25 19:11:33 -05:00
|
|
|
|
|
|
|
-- Sort rows based on more than one column with each column having different
|
|
|
|
-- sort direction.
|
|
|
|
SELECT * FROM person ORDER BY name ASC, age DESC;
|
2020-05-01 13:11:43 -04:00
|
|
|
+---+-----+----+
|
|
|
|
| id| name| age|
|
|
|
|
+---+-----+----+
|
|
|
|
|500| Dan| 50|
|
|
|
|
|400|Jerry|null|
|
|
|
|
|100| John| 30|
|
|
|
|
|200| Mary|null|
|
|
|
|
|300| Mike| 80|
|
|
|
|
+---+-----+----+
|
2020-05-10 13:57:25 -04:00
|
|
|
```
|
2020-01-29 09:41:40 -05:00
|
|
|
|
[SPARK-31383][SQL][DOC] Clean up the SQL documents in docs/sql-ref*
### What changes were proposed in this pull request?
This PR intends to clean up the SQL documents in `doc/sql-ref*`.
Main changes are as follows;
- Fixes wrong syntaxes and capitalize sub-titles
- Adds some DDL queries in `Examples` so that users can run examples there
- Makes query output in `Examples` follows the `Dataset.showString` (right-aligned) format
- Adds/Removes spaces, Indents, or blank lines to follow the format below;
```
---
license...
---
### Description
Writes what's the syntax is.
### Syntax
{% highlight sql %}
SELECT...
WHERE... // 4 indents after the second line
...
{% endhighlight %}
### Parameters
<dl>
<dt><code><em>Param Name</em></code></dt>
<dd>
Param Description
</dd>
...
</dl>
### Examples
{% highlight sql %}
-- It is better that users are able to execute example queries here.
-- So, we prepare test data in the first section if possible.
CREATE TABLE t (key STRING, value DOUBLE);
INSERT INTO t VALUES
('a', 1.0), ('a', 2.0), ('b', 3.0), ('c', 4.0);
-- query output has 2 indents and it follows the `Dataset.showString`
-- format (right-aligned).
SELECT * FROM t;
+---+-----+
|key|value|
+---+-----+
| a| 1.0|
| a| 2.0|
| b| 3.0|
| c| 4.0|
+---+-----+
-- Query statements after the second line have 4 indents.
SELECT key, SUM(value)
FROM t
GROUP BY key;
+---+----------+
|key|sum(value)|
+---+----------+
| c| 4.0|
| b| 3.0|
| a| 3.0|
+---+----------+
...
{% endhighlight %}
### Related Statements
* [XXX](xxx.html)
* ...
```
### Why are the changes needed?
The most changes of this PR are pretty minor, but I think the consistent formats/rules to write documents are important for long-term maintenance in our community
### Does this PR introduce any user-facing change?
Yes.
### How was this patch tested?
Manually checked.
Closes #28151 from maropu/MakeRightAligned.
Authored-by: Takeshi Yamamuro <yamamuro@apache.org>
Signed-off-by: Sean Owen <srowen@gmail.com>
2020-04-13 00:40:36 -04:00
|
|
|
### Related Statements
|
|
|
|
|
2020-05-10 13:57:25 -04:00
|
|
|
* [SELECT Main](sql-ref-syntax-qry-select.html)
|
|
|
|
* [WHERE Clause](sql-ref-syntax-qry-select-where.html)
|
|
|
|
* [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html)
|
|
|
|
* [HAVING Clause](sql-ref-syntax-qry-select-having.html)
|
|
|
|
* [SORT BY Clause](sql-ref-syntax-qry-select-sortby.html)
|
|
|
|
* [CLUSTER BY Clause](sql-ref-syntax-qry-select-clusterby.html)
|
|
|
|
* [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html)
|
|
|
|
* [LIMIT Clause](sql-ref-syntax-qry-select-limit.html)
|
2020-07-27 20:41:53 -04:00
|
|
|
* [CASE Clause](sql-ref-syntax-qry-select-case.html)
|
|
|
|
* [PIVOT Clause](sql-ref-syntax-qry-select-pivot.html)
|
|
|
|
* [LATERAL VIEW Clause](sql-ref-syntax-qry-select-lateral-view.html)
|