Value expressions are used in a variety of contexts, such as in the target list of the SELECT command, as new column values in INSERT or UPDATE, or in search conditions in a number of commands. The result of a value expression is sometimes called a scalar, to distinguish it from the result of a table expression (which is a table). Value expressions are therefore also called scalar expressions (or even simply expressions). The expression syntax allows the calculation of values from primitive parts using arithmetic, logical, set, and other operations.
A value expression is one of the following:
A constant or literal value; see Section 1.1.2.
A column reference.
A positional parameter reference, in the body of a function declaration.
An operator invocation.
A function call.
An aggregate expression.
A type cast.
A scalar subquery.
( expression )
Parentheses are used to group subexpressions and override precedence.
In addition to this list, there are a number of constructs that can be classified as an expression but do not follow any general syntax rules. These generally have the semantics of a function or operator and are explained in the appropriate location in Chapter 4. An example is the IS NULL clause.
We have already discussed constants in Section 1.1.2. The following sections discuss the remaining options.
A column can be referenced in the form:
correlation is either the name of a table, an alias for a table defined by means of a FROM clause, or the key words NEW or OLD. (NEW and OLD can only appear in the action portion of a rule, while other correlation names can be used in any SQL statement.) The correlation name and separating dot may be omitted if the column name is unique across all the tables being used in the current query. If column is of an array type, then the optional subscript selects a specific element or elements in the array. If no subscript is provided, then the whole array is selected. (See Chapter 6 for more about arrays.)
A positional parameter reference is used to indicate a parameter in an SQL function. Typically this is used in SQL function definition statements. The form of a parameter is:
For example, consider the definition of a function,
CREATE FUNCTION dept (text) RETURNS dept AS 'SELECT * FROM dept WHERE name = $1' LANGUAGE SQL;
Here the $1 will be replaced by the first function argument when the function is invoked.
There are three possible syntaxes for an operator invocation:
|expression operator expression (binary infix operator)|
|operator expression (unary prefix operator)|
|expression operator (unary postfix operator)|
The syntax for a function call is the name of a function (which is subject to the syntax rules for identifiers of Section 1.1.1), followed by its argument list enclosed in parentheses:
function ([expression [, expression ... ]] )
For example, the following computes the square root of 2:
The list of built-in functions is in Chapter 4. Other functions may be added by the user.
An aggregate expression represents the application of an aggregate function across the rows selected by a query. An aggregate function reduces multiple inputs to a single output value, such as the sum or average of the inputs. The syntax of an aggregate expression is one of the following:
|aggregate_name (ALL expression)|
|aggregate_name (DISTINCT expression)|
|aggregate_name ( * )|
The first form of aggregate expression invokes the aggregate
across all input rows for which the given expression yields a
non-NULL value. (Actually, it is up to the aggregate function
whether to ignore NULLs or not --- but all the standard ones
do.) The second form is the same as the first, since ALL is the default. The third form invokes the
aggregate for all distinct non-NULL values of the expression
found in the input rows. The last form invokes the aggregate
once for each input row regardless of NULL or non-NULL values;
since no particular input value is specified, it is generally
only useful for the
For example, count(*) yields the total number of input rows; count(f1) yields the number of input rows in which f1 is non-NULL; count(distinct f1) yields the number of distinct non-NULL values of f1.
The predefined aggregate functions are described in Section 4.14. Other aggregate functions may be added by the user.
A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent syntaxes for type casts:
CAST ( expression AS type ) expression::type
The CAST syntax conforms to SQL92; the syntax with :: is historical PostgreSQL usage.
When a cast is applied to a value expression of a known type, it represents a run-time type conversion. The cast will succeed only if a suitable type conversion function is available. Notice that this is subtly different from the use of casts with constants, as shown in Section 22.214.171.124. A cast applied to an unadorned string literal represents the initial assignment of a type to a literal constant value, and so it will succeed for any type (if the contents of the string literal are acceptable input syntax for the data type).
An explicit type cast may be omitted if there is no ambiguity as to the type that a value expression must produce (for example, when it is assigned to a table column); the system will automatically apply a type cast in such cases.
It is also possible to specify a type cast using a function-like syntax:
typename ( expression )
However, this only works for types whose names are also valid as function names. For example, double precision can't be used this way, but the equivalent float8 can. Also, the names interval, time, and timestamp can only be used in this fashion if they are double-quoted, because of parser conflicts. Therefore, the use of the function-like cast syntax leads to inconsistencies and should probably be avoided in new applications.
A scalar subquery is an ordinary SELECT in parentheses that returns exactly one row with one column. The SELECT query is executed and the single returned value is used in the surrounding value expression. It is an error to use a query that returns more than one row or more than one column as a scalar subquery. (But if, during a particular execution, the subquery returns no rows, there is no error; the scalar result is taken to be NULL.) The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery. See also Section 4.15.
For example, the following finds the largest city population in each state:
SELECT name, (SELECT max(pop) FROM cities WHERE cities.state = states.name) FROM states;