Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 117 additions & 1 deletion docs/sql-ref-syntax-qry-select-orderby.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,121 @@ license: |
See the License for the specific language governing permissions and
limitations under the License.
---
The <code>ORDER BY</code> clause is used to return the result rows in a sorted manner
in the user specified order. Unlike the <code>SORT BY</code> clause, this clause guarantees
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to SORT BY?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huaxingao will do it in the finalization pr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK if you want to take care of that at the end

total order in the output.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

total order -> a total order?


**This page is under construction**
### Syntax
{% highlight sql %}
ORDER BY { expression [ sort_direction | nulls_sort_oder ] [ , ...] }
{% endhighlight %}

### Parameters
<dl>
<dt><code><em>ORDER BY</em></code></dt>
<dd>
Specifies a comma-separated list of expressions along with optional parameters <code>sort_direction</code>
and <code>nulls_sort_order</code> which are used to sort the rows.
</dd>
<dt><code><em>sort_direction</em></code></dt>
<dd>
Optionally specifies whether to sort the rows in ascending (lowest to highest) or descending
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the statement (lowest to highest)? ascending or descending is enough?

(highest to lowest) order. The valid values for sort direction are <code>ASC</code> for ascending
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort direction -> the sort direction?

and <code>DESC</code> for descending. If sort direction is not explicitly specified then by default
rows are sorted in ascending manner. <br><br>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just "sorted ascending"

<b>Syntax:</b>
<code>
[ ASC | DESC ]
</code>
</dd>
<dt><code><em>nulls_sort_order</em></code></dt>
<dd>
Optionally specifies whether NULL values are returned before/after non-NULL values, based on the
sort direction. In Spark, NULL values are considered to be lower than any non-NULL values by default.
Therefore the ordering of NULL values depend on the sort direction.<br><br>
<ol>
<li>If the sort order is ASC, NULLS are returned first; to force NULLS to be last, use NULLS LAST</li>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might turn this around, to emphasize the null sort order: "If NULLS FIRST (the default), then NULLs are returned first if sort order is ASC, and last if sort order is DESC" and likewise for the second items.

Copy link
Contributor Author

@dilipbiswal dilipbiswal Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srowen Hmmn.. trying to think if we are conveying properly with the above re-wording. So

  • If if NULLS FIRST is explicitly specified, you would ALWAYS see NULLs at the top of your resultset no matter what the sort order is.
  • if NULLS LAST is explicitly specified , you would ALWAYS see NULLS at the end no matter what the sort order is

.What do you think ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my mistake, is that how it works? so NULLS FIRST puts nulls first no matter what the sort order? Maybe we can take the focus off of the sort order then. For example NULLS LAST does the same thing no matter what the sort order.

Maybe like: "NULLS FIRST forces NULLs to sort before all non-NULL values, regardless of the sort order" and likewise for NULLS LAST, and then explain that, if not specified, NULLs sort first if ASC and last if DESC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srowen Got it.. Will make the change.

<li>If the sort order is DESC, NULLS are returned last; to force NULLS to be first, use NULLS FIRST</li>
</ol><br>
<b>Syntax:</b>
<code>
[ NULLS { FIRST | LAST } ]
</code>
</dd>
</dl>

### Examples
{% highlight sql %}
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);

-- Sort rows by age. By default rows are sorted in ascending manner.
SELECT name, age FROM person ORDER BY age;

+-----+----+
|name |age |
+-----+----+
|Jerry|null|
|Mary |null|
|John |30 |
|Dan |50 |
|Mike |80 |
+-----+----+

-- Sort rows in ascending manner keeping null values to be last.
SELECT name, age FROM person ORDER BY age NULLS LAST;

+-----+----+
|name |age |
+-----+----+
|John |30 |
|Dan |50 |
|Mike |80 |
|Mary |null|
|Jerry|null|
+-----+----+

-- Sort rows by age in descending manner.
SELECT name, age FROM person ORDER BY age DESC;

+-----+----+
|name |age |
+-----+----+
|Mike |80 |
|Dan |50 |
|John |30 |
|Jerry|null|
|Mary |null|
+-----+----+

-- Sort rows in ascending manner keeping null values to be first.
SELECT name, age FROM person ORDER BY age DESC NULLS FIRST;

+-----+----+
|name |age |
+-----+----+
|Jerry|null|
|Mary |null|
|Mike |80 |
|Dan |50 |
|John |30 |
+-----+----+

-- Sort rows based on more than one column with each column having different
-- sort direction.
SELECT * FROM person ORDER BY name ASC, age DESC;

+---+-----+----+
|id |name |age |
+---+-----+----+
|500|Dan |50 |
|400|Jerry|null|
|100|John |30 |
|200|Mary |null|
|300|Mike |80 |
+---+-----+----+
{% endhighlight %}