Project: YugabyteDB (App Definition and Development / Database)
File: dml_select.md

title: SELECT statement [YSQL] headerTitle: SELECT linkTitle: SELECT description: Use the SELECT statement to retrieve rows of specified columns that meet a given condition from a table. menu: v2.18: identifier: dml_select parent: statements type: docs Use the `SELECT` statement to retrieve rows of specified columns that meet a given condition from a table. It specifies the columns to be retrieved, the name of the table, and the condition each selected row must satisfy. The same syntax rules govern a subquery, wherever you might use onelike, for example, in an . Certain syntax spots, for example a `WHERE` clause predicate or the actual argument of a function like `sqrt()`, allow only a scalar subquery. {{%ebnf%}} select, with_clause, select_list, trailingselectclauses, commontableexpression, fnoverwindow, ordinaryaggregatefn_invocation, withingroupaggregatefninvocation, grouping_element, order_expr {{%/ebnf%}} See the section for more information about the semantics of the `commontableexpression` grammar rule. An error is raised if the specified `table_name` does not exist. `*` represents all columns. While the where clause allows a wide range of operators, the exact conditions used in the where clause have significant performance considerations (especially for large datasets). For details on `fromitem` and `withquery` see in the PostgreSQL documentation. The `fnoverwindow` rule denotes the special kind of `SELECT` list item that must be used to invoke a window function and that may be used to invoke an aggregate function. (Window functions are known as analytic functions in the terminology of some SQL database systems.) The dedicated diagram that follows the main diagram for the `select` rule shows the `FILTER` and the `OVER` keywords. You can see that you cannot invoke a function in this way without specifying an `OVER` clauseand that the `OVER` clause requires the specification of the so-called that gives this invocation style its name. The `FILTER` clause is optional and may be used only when you invoke an aggregate function in this way. All of this is explained in the section within the major section . The `ordinaryaggregatefninvocation` rule and the `withingroupaggregatefn_invocation` rule denote the special kinds of `SELECT` list item that are used to invoke an aggregate function (when it isn't invoked as a window function). When an aggregate function is invoked in either of these two ways, it's very common to do so in conjunction with the `GROUP BY` and `HAVING` clauses. All of this is explained in the section within the major section . When you understand the story of the invocation of these two kinds of functions from the accounts in the section and the section, you can use the `\df` meta-command in `ysqlsh` to discover the status of a particular function, thus: ``` \df row_number ... | Argument data types | Type ... +-+-- ... | | window \df rank

| Argument data types | Type ... +-+-- ... | | window ... | VARIADIC "any" ORDER BY VARIADIC "any" | agg \df avg ... | Argument data types | Type ... +-+-- ... | bigint | agg ... | <other data types> | agg ``` A function whose type is listed as "window" can be invoked only as a window function. See this account of . A function whose type is listed both as "window" and as agg can be invoked: either as a window function using the `fnoverwindow` syntaxsee this account of or as a so-called using the `withingroupaggregatefninvocation` syntaxsee this account of . A function whose type is listed only as "agg" can, in fact, be invoked either as an aggregate function using the `ordinaryaggregatefninvocation` syntax or as a window function using the `fnoverwindow` syntax. The `avg()` function is described in the "Aggregate functions" major section in the subsection . See its subsections and for, respectively, the `ordinaryaggregatefninvocation` and the `fnoverwindow` invocation alternatives. Notice that the three functions , , and are exceptions to this general rule (and they are the only exceptions). These functions are referred to as . `\df` lists the type of these functions only as "agg". But these cannot be invoked as window functions. The attempt causes this error: ``` 42809: WITHIN GROUP is required for ordered-set aggregate mode ``` Note: The documentation in the major section usually refers to the syntax that the `ordinaryaggregatefninvocation` rule and the `withingroupaggregatefninvocation` rule jointly govern as the because it's these two syntax variants (and only these two) can be used together with the `GROUP BY` clause (and therefore the `HAVING` clause). And it usually refers to the syntax that the `fnoverwindow` rule governs as the because this syntax variant requires_ the use of the `OVER` clause. Moreover, the use of the `GROUP BY` clause (and therefore the `HAVING` clause) is illegal with this syntax variant. Create two sample tables. ```plpgsql yugabyte=# CREATE TABLE sample1(k1 bigint, k2 float, v text, PRIMARY KEY (k1, k2)); ``` ```plpgsql yugabyte=# CREATE TABLE sample2(k1 bigint, k2 float, v text, PRIMARY KEY (k1, k2)); ``` Insert some rows. ```plpgsql yugabyte=# INSERT INTO sample1(k1, k2, v) VALUES (1, 2.5, 'abc'), (1, 3.5, 'def'), (1, 4.5, 'xyz'); ``` ```plpgsql yugabyte=# INSERT INTO sample2(k1, k2, v) VALUES (1, 2.5, 'foo'), (1, 4.5, 'bar'); ``` Select from both tables using join. ```plpgsql yugabyte=# SELECT a.k1, a.k2, a.v as av, b.v as bv FROM sample1 a LEFT JOIN sample2 b ON (a.k1 = b.k1 and a.k2 = b.k2) WHERE a.k1 = 1 AND a.k2 IN (2.5, 3.5) ORDER BY a.k2 DESC; ``` ``` k1 | k2 | av | bv -+--+--+-- 1 | 3.5 | def | 1 | 2.5 | abc | foo (2 rows) ```