A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/constructs/order-by below:

Website Navigation


ORDER BY | Snowflake Documentation

Categories:

Query syntax

ORDER BY

Specifies an ordering of the rows of the result table from a SELECT list.

Syntax

Sorting by specific columns

SELECT ...
  FROM ...
  ORDER BY orderItem [ , orderItem , ... ]
  [ ... ]

Copy

Where:

orderItem ::= { <column_alias> | <position> | <expr> } [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ]

Copy

Sorting by all columns

SELECT ...
  FROM ...
  ORDER BY ALL [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ]
  [ ... ]

Copy

Parameters
column_alias

Column alias appearing in the query block’s SELECT list.

position

Position of an expression in the SELECT list.

expr

Any expression on tables in the current scope.

{ ASC | DESC }

Optionally returns the values of the sort key in ascending (lowest to highest) or descending (highest to lowest) order.

Default: ASC

NULLS { FIRST | LAST }

Optionally specifies whether NULL values are returned before/after non-NULL values, based on the sort order (ASC or DESC).

Default: Depends on the sort order (ASC or DESC); see the usage notes below for details

ALL

Sorts the results by all of the columns specified in the SELECT list. The results are sorted by the columns in the order in which they appear.

For example, suppose that the SELECT list contains:

SELECT col_1, col_2, col_3
  FROM my_table
  ORDER BY ALL;

Copy

The results are sorted first by col_1, then by col_2, and then by col_3.

Note

You cannot specify ORDER BY ALL if a column in the SELECT list uses an aggregate function.

Usage notes Examples

The following examples demonstrate how to use ORDER BY to sort the results:

Sorting by string values

The following example sorts the results by string values:

SELECT column1
  FROM VALUES
    ('a'), ('1'), ('B'), (null), ('2'), ('01'), ('05'),
    (' this'), ('this'), ('this and that'), ('&'), ('%')
  ORDER BY column1;

Copy

+---------------+
| COLUMN1       |
|---------------|
|  this         |
| %             |
| &             |
| 01            |
| 05            |
| 1             |
| 2             |
| B             |
| a             |
| this          |
| this and that |
| NULL          |
+---------------+
Sorting by numeric values

The following example sorts the results by numeric values:

SELECT column1
  FROM VALUES
    (3), (4), (null), (1), (2), (6),
    (5), (0005), (.05), (.5), (.5000)
  ORDER BY column1;

Copy

+---------+
| COLUMN1 |
|---------|
|    0.05 |
|    0.50 |
|    0.50 |
|    1.00 |
|    2.00 |
|    3.00 |
|    4.00 |
|    5.00 |
|    5.00 |
|    6.00 |
|    NULL |
+---------+
Sorting NULLS first or last

The following example configures all queries in the session to sort NULLS last by setting the DEFAULT_NULL_ORDERING parameter to LAST.

ALTER SESSION SET DEFAULT_NULL_ORDERING = 'LAST';

Copy

SELECT column1
  FROM VALUES (1), (null), (2), (null), (3)
  ORDER BY column1;

Copy

+---------+
| COLUMN1 |
|---------|
|       1 |
|       2 |
|       3 |
|    NULL |
|    NULL |
+---------+
SELECT column1
  FROM VALUES (1), (null), (2), (null), (3)
  ORDER BY column1 DESC;

Copy

+---------+
| COLUMN1 |
|---------|
|    NULL |
|    NULL |
|       3 |
|       2 |
|       1 |
+---------+

The following example overrides the DEFAULT_NULL_ORDERING parameter by specifying NULLS FIRST in a query:

SELECT column1
  FROM VALUES (1), (null), (2), (null), (3)
  ORDER BY column1 NULLS FIRST;

Copy

+---------+
| COLUMN1 |
|---------|
|    NULL |
|    NULL |
|       1 |
|       2 |
|       3 |
+---------+

The following example sets the DEFAULT_NULL_ORDERING parameter to FIRST to sort NULLS first:

ALTER SESSION SET DEFAULT_NULL_ORDERING = 'FIRST';

Copy

SELECT column1
  FROM VALUES (1), (null), (2), (null), (3)
  ORDER BY column1;

Copy

+---------+
| COLUMN1 |
|---------|
|    NULL |
|    NULL |
|       1 |
|       2 |
|       3 |
+---------+
SELECT column1
  FROM VALUES (1), (null), (2), (null), (3)
  ORDER BY column1 DESC;

Copy

+---------+
| COLUMN1 |
|---------|
|       3 |
|       2 |
|       1 |
|    NULL |
|    NULL |
+---------+

The following example overrides the DEFAULT_NULL_ORDERING parameter by specifying NULLS LAST in a query:

SELECT column1
  FROM VALUES (1), (null), (2), (null), (3)
  ORDER BY column1 NULLS LAST;

Copy

+---------+
| COLUMN1 |
|---------|
|       1 |
|       2 |
|       3 |
|    NULL |
|    NULL |
+---------+
Sorting by all columns in the SELECT list

To run the examples in this section, create the following table:

CREATE OR REPLACE TABLE my_sort_example(a NUMBER, s VARCHAR, b BOOLEAN);

INSERT INTO my_sort_example VALUES
  (0, 'abc', TRUE),
  (0, 'abc', FALSE),
  (0, 'abc', NULL),
  (0, 'xyz', FALSE),
  (0, NULL, FALSE),
  (1, 'xyz', TRUE),
  (NULL, 'xyz', FALSE);

Copy

The following example sorts the results by all columns in the table:

SELECT * FROM my_sort_example
  ORDER BY ALL;

Copy

As shown below, the results are sorted first by the a column, then by the s column, and then by the b column (the order in which the columns were defined in the table).

+------+------+-------+
| A    | S    | B     |
|------+------+-------|
| 0    | abc  | False |
| 0    | abc  | True  |
| 0    | abc  | NULL  |
| 0    | xyz  | False |
| 0    | NULL | False |
| 1    | xyz  | True  |
| NULL | xyz  | False |
+------+------+-------+

The following example sorts the results in ascending order:

SELECT * FROM my_sort_example
  ORDER BY ALL ASC;

Copy

+------+------+-------+
| A    | S    | B     |
|------+------+-------|
| 0    | abc  | False |
| 0    | abc  | True  |
| 0    | abc  | NULL  |
| 0    | xyz  | False |
| 0    | NULL | False |
| 1    | xyz  | True  |
| NULL | xyz  | False |
+------+------+-------+

The following example sets the DEFAULT_NULL_ORDERING parameter to sort NULL values last for all queries executed during the session:

ALTER SESSION SET DEFAULT_NULL_ORDERING = 'LAST';

Copy

SELECT * FROM my_sort_example
  ORDER BY ALL;

Copy

+------+------+-------+
| A    | S    | B     |
|------+------+-------|
| NULL | xyz  | False |
| 0    | NULL | False |
| 0    | abc  | NULL  |
| 0    | abc  | False |
| 0    | abc  | True  |
| 0    | xyz  | False |
| 1    | xyz  | True  |
+------+------+-------+

The following example specifies NULLS FIRST in a query to override that setting:

SELECT * FROM my_sort_example
  ORDER BY ALL NULLS FIRST;

Copy

+------+------+-------+
| A    | S    | B     |
|------+------+-------|
| NULL | xyz  | False |
| 0    | NULL | False |
| 0    | abc  | NULL  |
| 0    | abc  | False |
| 0    | abc  | True  |
| 0    | xyz  | False |
| 1    | xyz  | True  |
+------+------+-------+

The following example returns the columns in the order b, s, and a. The results are sorted first by b, then by s, and then by a:

SELECT b, s, a FROM my_sort_example
  ORDER BY ALL NULLS LAST;

Copy

+-------+------+------+
| B     | S    | A    |
|-------+------+------|
| False | abc  | 0    |
| False | xyz  | 0    |
| False | xyz  | NULL |
| False | NULL | 0    |
| True  | abc  | 0    |
| True  | xyz  | 1    |
| NULL  | abc  | 0    |
+-------+------+------+

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4