---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://ydb.tech/docs/en/yql/reference/syntax/select.md?version=v25.4
  - https://ydb.tech/docs/ru/yql/reference/syntax/select.md?version=v25.4
  - href: en/yql/reference/syntax/select/index.md
    type: text/markdown
    title: Markdown version
  - href: ../../../../llms.txt
    type: text/markdown
    title: llms.txt
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?version=v25.4) clauses.
* Execute [FLATTEN COLUMNS](https://ydb.tech/docs/en/yql/reference/syntax/select/flatten.md?version=v25.4#flatten-columns) or [FLATTEN BY](https://ydb.tech/docs/en/yql/reference/syntax/select/flatten.md?version=v25.4); aliases set in `FLATTEN BY` become visible after this point.

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

* 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?version=v25.4).
* Execute [WHERE](https://ydb.tech/docs/en/yql/reference/syntax/select/where.md?version=v25.4) &mdash; Discard all the data mismatching the predicate.
* Execute [GROUP BY](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md?version=v25.4), evaluate aggregate functions.
* Apply the filter [HAVING](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md?version=v25.4#having).

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

* 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?version=v25.4) to the resulting columns.
* Execute similarly every subquery inside [UNION ALL](https://ydb.tech/docs/en/yql/reference/syntax/select/union.md?version=v25.4#union-all), combine them (see [PRAGMA AnsiOrderByLimitInUnionAll](https://ydb.tech/docs/en/yql/reference/syntax/pragma.md?version=v25.4#pragmas)).
* Perform sorting with [ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/order_by.md?version=v25.4).
* Apply [OFFSET and LIMIT](https://ydb.tech/docs/en/yql/reference/syntax/select/limit_offset.md?version=v25.4) 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?version=v25.4#union-all) and positional [ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/order_by.md?version=v25.4) (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?version=v25.4): 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?version=v25.4#union-all) execution mode.
* The column order for [AS_TABLE](https://ydb.tech/docs/en/yql/reference/syntax/select/from_as_table.md?version=v25.4) 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?version=v25.4)
* [FROM AS_TABLE](https://ydb.tech/docs/en/yql/reference/syntax/select/from_as_table.md?version=v25.4)
* [FROM SELECT](https://ydb.tech/docs/en/yql/reference/syntax/select/from_select.md?version=v25.4)
* [DISTINCT](https://ydb.tech/docs/en/yql/reference/syntax/select/distinct.md?version=v25.4)
* [UNIQUE DISTINCT](https://ydb.tech/docs/en/yql/reference/syntax/select/unique_distinct_hints.md?version=v25.4)
* [UNION](https://ydb.tech/docs/en/yql/reference/syntax/select/union.md?version=v25.4)
* [WITH](https://ydb.tech/docs/en/yql/reference/syntax/select/with.md?version=v25.4)
* [WITHOUT](https://ydb.tech/docs/en/yql/reference/syntax/select/without.md?version=v25.4)
* [WHERE](https://ydb.tech/docs/en/yql/reference/syntax/select/where.md?version=v25.4)
* [ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/order_by.md?version=v25.4)
* [ASSUME ORDER BY](https://ydb.tech/docs/en/yql/reference/syntax/select/assume_order_by.md?version=v25.4)
* [LIMIT OFFSET](https://ydb.tech/docs/en/yql/reference/syntax/select/limit_offset.md?version=v25.4)
* [JOIN](https://ydb.tech/docs/en/yql/reference/syntax/select/join.md?version=v25.4)
* [GROUP BY](https://ydb.tech/docs/en/yql/reference/syntax/select/group-by.md?version=v25.4)
* [FLATTEN](https://ydb.tech/docs/en/yql/reference/syntax/select/flatten.md?version=v25.4)
* [WINDOW](https://ydb.tech/docs/en/yql/reference/syntax/select/window.md?version=v25.4)






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

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

