---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://ydb.tech/docs/en/yql/reference/syntax/select.md
  - https://ydb.tech/docs/ru/yql/reference/syntax/select.md
sourcePath: en/core/yql/reference/syntax/select/index.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb.tech/docs/en/llms.txt

<!-- markdownlint-disable blanks-around-fences -->

# SELECT

Returns the result of evaluating the expressions specified after `SELECT`.

It can be used in combination with other operations to obtain other effect.

## Examples

```yql
SELECT "Hello, world!";
```

```yql
SELECT 2 + 2;
```

## SELECT execution procedure {#selectexec}

The `SELECT` query result is calculated as follows:

* Determine the set of input tables by evaluating the [FROM](https://ydb.tech/docs/en/yql/reference/syntax/select/from.md) clauses.
* Execute [FLATTEN COLUMNS](https://ydb.tech/docs/en/yql/reference/syntax/select/flatten.md#flatten-columns) or [FLATTEN BY](https://ydb.tech/docs/en/yql/reference/syntax/select/flatten.md); aliases set in `FLATTEN BY` become visible after this point.

* Execute every [JOIN](https://ydb.tech/docs/en/yql/reference/syntax/select/join.md).

* Add to (or replace in) the data the columns listed in [GROUP BY ... AS ...](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md).
* Execute [WHERE](https://ydb.tech/docs/en/yql/reference/syntax/select/where.md) &mdash; Discard all the data mismatching the predicate.
* Execute [GROUP BY](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md), evaluate aggregate functions.
* Apply the filter [HAVING](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md#having).

* Evaluate [window functions](https://ydb.tech/docs/en/yql/reference/syntax/select/window.md);

* Evaluate expressions in `SELECT`.
* Assign names set by aliases to expressions in `SELECT`.
* Apply top-level [DISTINCT](https://ydb.tech/docs/en/yql/reference/syntax/select/distinct.md) to the resulting columns.
* Execute similarly every subquery inside [UNION ALL](https://ydb.tech/docs/en/yql/reference/syntax/select/union.md#union-all), combine them (see [PRAGMA AnsiOrderByLimitInUnionAll](https://ydb.tech/docs/en/yql/reference/syntax/pragma.md#pragmas)).
* Perform sorting with [ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/order_by.md).
* Apply [OFFSET and LIMIT](https://ydb.tech/docs/en/yql/reference/syntax/select/limit_offset.md) to the result.

## Column order in YQL {#orderedcolumns}

The standard SQL is sensitive to the order of columns in projections (that is, in `SELECT`). While the order of columns must be preserved in the query results or when writing data to a new table, some SQL constructs use this order.
This applies, for example, to [UNION ALL](https://ydb.tech/docs/en/yql/reference/syntax/select/union.md#union-all) and positional [ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/order_by.md) (ORDER BY ordinal).

The column order is ignored in YQL by default:

* The order of columns in the output tables and query results is undefined
* The data scheme of the `UNION ALL` result is output by column names rather than positions

If you enable `PRAGMA OrderedColumns;`, the order of columns is preserved in the query results and is derived from the order of columns in the input tables using the following rules:

* `SELECT`: an explicit column enumeration dictates the result order.
* `SELECT` with an asterisk (`SELECT * FROM ...`) inherits the order from its input.

* The order of columns after [JOIN](https://ydb.tech/docs/en/yql/reference/syntax/select/join.md): First output the left-hand columns, then the right-hand ones. If the column order in any of the sides in the `JOIN` output is undefined, the column order in the result is also undefined.


* The order in `UNION ALL` depends on the [UNION ALL](https://ydb.tech/docs/en/yql/reference/syntax/select/union.md#union-all) execution mode.
* The column order for [AS_TABLE](https://ydb.tech/docs/en/yql/reference/syntax/select/from_as_table.md) is undefined.

{% note warning %}

In the YT table schema, key columns always precede non-key columns. The order of key columns is determined by the order of the composite key.
When `PRAGMA OrderedColumns;` is enabled, non-key columns preserve their output order.

{% endnote %}

## Combining queries {#combining-queries}

Results of several SELECT statements (or subqueries) can be combined using `UNION` and `UNION ALL` keywords.

```yql
query1 UNION [ALL] query2 (UNION [ALL] query3 ...)
```

Union of more than two queries is interpreted as a left-associative operation, that is

```yql
query1 UNION query2 UNION ALL query3
```

is interpreted as

```yql
(query1 UNION query2) UNION ALL query3
```

If the underlying queries have one of the `ORDER BY/LIMIT/DISCARD/INTO RESULT` operators, the following rules apply:

* `ORDER BY/LIMIT/INTO RESULT` is only allowed after the last query
* `DISCARD` is only allowed before the first query
* the operators apply to the `UNION [ALL]` as a whole, instead of referring to one of the queries
* to apply the operator to one of the queries, enclose the query in parentheses

## Clauses supported in SELECT

* [FROM](https://ydb.tech/docs/en/yql/reference/syntax/select/from.md)
* [FROM AS_TABLE](https://ydb.tech/docs/en/yql/reference/syntax/select/from_as_table.md)
* [FROM SELECT](https://ydb.tech/docs/en/yql/reference/syntax/select/from_select.md)
* [DISTINCT](https://ydb.tech/docs/en/yql/reference/syntax/select/distinct.md)
* [UNIQUE DISTINCT](https://ydb.tech/docs/en/yql/reference/syntax/select/unique_distinct_hints.md)
* [UNION](https://ydb.tech/docs/en/yql/reference/syntax/select/union.md)
* [WITH](https://ydb.tech/docs/en/yql/reference/syntax/select/with.md)
* [WITHOUT](https://ydb.tech/docs/en/yql/reference/syntax/select/without.md)
* [WHERE](https://ydb.tech/docs/en/yql/reference/syntax/select/where.md)
* [ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/order_by.md)
* [ASSUME ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/assume_order_by.md)
* [LIMIT OFFSET](https://ydb.tech/docs/en/yql/reference/syntax/select/limit_offset.md)
* [JOIN](https://ydb.tech/docs/en/yql/reference/syntax/select/join.md)
* [GROUP BY](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md)
* [FLATTEN](https://ydb.tech/docs/en/yql/reference/syntax/select/flatten.md)
* [WINDOW](https://ydb.tech/docs/en/yql/reference/syntax/select/window.md)






* [VIEW secondary_index](https://ydb.tech/docs/en/yql/reference/syntax/select/secondary_index.md)

* [VIEW vector_index](https://ydb.tech/docs/en/yql/reference/syntax/select/vector_index.md)

