RefLib

Reference library for AI

Back

postgresql_16:7.1.170.1 Description

PostgreSQL 16 > Reference > SQL Commands > SELECT > Description

SELECT retrieves rows from zero or more tables. The general processing of SELECT is as follows: All queries in the WITH list are computed. These effectively serve as temporary tables that can be referenced in the FROM list. A WITH query that is referenced more than once in FROM is computed only once, unless specified otherwise with NOT MATERIALIZED. (See (see postgresql_16:7.1.170.2) below.) All elements in the FROM list are computed. (Each element in the FROM list is a real or virtual table.) If more than one element is specified in the FROM list, they are cross-joined together. (See (see postgresql_16:7.1.170.2) below.) If the WHERE clause is specified, all rows that do not satisfy the condition are eliminated from the output. (See (see postgresql_16:7.1.170.2) 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 (see postgresql_16:7.1.170.2) and (see postgresql_16:7.1.170.2) below.) Although query output columns are nominally computed in the next step, they can also be referenced (by name or ordinal number) in the GROUP BY clause. The actual output rows are computed using the SELECT output expressions for each selected row or row group. (See (see postgresql_16:7.1.170.2) below.) SELECT DISTINCT eliminates duplicate rows from the result. SELECT DISTINCT ON eliminates rows that match on all the specified expressions. SELECT ALL (the default) will return all candidate rows, including duplicates. (See (see postgresql_16:7.1.170.2) below.) Using the operators UNION, INTERSECT, and EXCEPT, the output of more than one SELECT statement can be combined to form a single result set. The UNION operator returns all rows that are in one or both of the result sets. The INTERSECT operator returns all rows that are strictly in both result sets. The EXCEPT operator returns the rows that are in the first result set but not in the second. In all three cases, duplicate rows are eliminated unless ALL is specified. The noise word DISTINCT can be added to explicitly specify eliminating duplicate rows. Notice that DISTINCT is the default behavior here, even though ALL is the default for SELECT itself. (See (see postgresql_16:7.1.170.2), (see postgresql_16:7.1.170.2), and (see postgresql_16:7.1.170.2) below.) 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 (see postgresql_16:7.1.170.2) below.) If the LIMIT (or FETCH FIRST) or OFFSET clause is specified, the SELECT statement only returns a subset of the result rows. (See (see postgresql_16:7.1.170.2) below.) If FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE or FOR KEY SHARE is specified, the SELECT statement locks the selected rows against concurrent updates. (See (see postgresql_16:7.1.170.2) below.)

You must have SELECT privilege on each column used in a SELECT command. The use of FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE or FOR KEY SHARE requires UPDATE privilege as well (for at least one column of each table so selected).

Back