--- mapped_pages: - https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-syntax-select.html --- # SELECT [sql-syntax-select] ```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: 1. 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 [FROM Clause](#sql-syntax-from) below). 2. If the `WHERE` clause is specified, all rows that do not satisfy the condition are eliminated from the output. (See [WHERE Clause](#sql-syntax-where) below.) 3. 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 [GROUP BY Clause](#sql-syntax-group-by) and [HAVING Clause](#sql-syntax-having) below.) 4. The actual output rows are computed using the `SELECT` output expressions for each selected row or row group. 5. 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 [ORDER BY Clause](#sql-syntax-order-by) below.) 6. 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 [LIMIT Clause](#sql-syntax-limit) and [TOP clause](#sql-syntax-top) below.) ## `SELECT` List [sql-syntax-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 : ```sql SELECT 1 + 1 AS result; result --------------- 2 ``` 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 Elasticsearch SQL if no name is given: ```sql SELECT 1 + 1; 1 + 1 -------------- 2 ``` or if it’s a simple column reference, use its name as the column name: ```sql SELECT emp_no FROM emp LIMIT 1; emp_no --------------- 10001 ``` ## Wildcard [sql-syntax-select-wildcard] To select all the columns in the source, one can use `*`: ```sql SELECT * FROM emp LIMIT 1; birth_date | emp_no | first_name | gender | hire_date | languages | last_name | name | salary --------------------+---------------+---------------+---------------+------------------------+---------------+---------------+---------------+--------------- 1953-09-02T00:00:00Z|10001 |Georgi |M |1986-06-26T00:00:00.000Z|2 |Facello |Georgi Facello |57305 ``` which essentially returns all(top-level fields, sub-fields, such as multi-fields are ignored] columns found. ## TOP [sql-syntax-top] The `TOP` clause can be used before the [`SELECT` list](#sql-syntax-select-list) or the < to restrict (limit) the number of rows returned using the format: ```sql SELECT TOP