[role="xpack"] [testenv="basic"] [[sql-syntax-select]] === SELECT .Synopsis: [source, sql] ---- SELECT [TOP [ count ] ] select_expr [, ...] [ FROM table_name ] [ WHERE condition ] [ GROUP BY grouping_element [, ...] ] [ HAVING condition] [ ORDER BY expression [ ASC | DESC ] [, ...] ] [ LIMIT [ count ] ] [ PIVOT ( aggregation_expr FOR column IN ( value [ [ AS ] alias ] [, ...] ) ) ] ---- *Description*: Retrieves rows from zero or more tables. The general execution of `SELECT` is as follows: . All elements in the `FROM` list are computed (each element can be base or alias table). Currently `FROM` supports exactly one table. Do note however that the table name can be a pattern (see <> below). . If the `WHERE` clause is specified, all rows that do not satisfy the condition are eliminated from the output. (See <> below.) . If the `GROUP BY` clause is specified, or if there are aggregate function calls, the output is combined into groups of rows that match on one or more values, and the results of aggregate functions are computed. If the `HAVING` clause is present, it eliminates groups that do not satisfy the given condition. (See <> and <> below.) . The actual output rows are computed using the `SELECT` output expressions for each selected row or row group. . If the `ORDER BY` clause is specified, the returned rows are sorted in the specified order. If `ORDER BY` is not given, the rows are returned in whatever order the system finds fastest to produce. (See <> below.) . If the `LIMIT` or `TOP` is specified (cannot use both in the same query), the `SELECT` statement only returns a subset of the result rows. (See <> and <> below.) [[sql-syntax-select-list]] ==== `SELECT` List `SELECT` list, namely the expressions between `SELECT` and `FROM`, represent the output rows of the `SELECT` statement. As with a table, every output column of a `SELECT` has a name which can be either specified per column through the `AS` keyword : [source, sql] ---- include-tagged::{sql-specs}/docs/docs.csv-spec[selectColumnAlias] ---- Note: `AS` is an optional keyword however it helps with the readability and in some case ambiguity of the query which is why it is recommended to specify it. assigned by {es-sql} if no name is given: [source, sql] ---- include-tagged::{sql-specs}/docs/docs.csv-spec[selectInline] ---- or if it's a simple column reference, use its name as the column name: [source, sql] ---- include-tagged::{sql-specs}/docs/docs.csv-spec[selectColumn] ---- [[sql-syntax-select-wildcard]] ==== Wildcard To select all the columns in the source, one can use `*`: [source, sql] ---- include-tagged::{sql-specs}/docs/docs.csv-spec[wildcardWithOrder] ---- which essentially returns all(top-level fields, sub-fields, such as multi-fields are ignored] columns found. [[sql-syntax-top]] ==== TOP The `TOP` clause can be used before the <> or the < to restrict (limit) the number of rows returned using the format: [source, sql] ---- SELECT TOP