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 the Section called Constants.
A column reference
An operator invocation:
expression operator expression (binary infix operator) |
operator expression (unary prefix operator) |
expression operator (unary postfix operator) |
( expression ) Parentheses are used to group subexpressions and override precedence.
A positional parameter reference, in the body of a function declaration.
A function call
An aggregate expression
A type cast
A scalar subquery. This is an ordinary SELECT in parentheses that returns exactly one row with one column. If you attempt to use a subquery that returns more than one row or more than one column in the context of a value expression, you will receive an error.
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 3. An example is the IS NULL clause.
We have already discussed constants in the Section called Constants. The following sections discuss the remaining options.
A column can be referenced in the form:
correlation.columnname `['subscript`]' |
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:
$number |
For example, consider the definition of a function, dept, as
CREATE FUNCTION dept (text) RETURNS dept AS 'select * from dept where name = $1' LANGUAGE 'sql'; |
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 the Section called Identifiers and Key Words), followed by its argument list enclosed in parentheses:
function ([expression [, expression ... ]]) |
For example, the following computes the square root of 2:
sqrt(2) |
The list of built-in functions is in Chapter 3. 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 (expression) |
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 useful only for the count() aggregate function.
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 the Section called Aggregate Functions in Chapter 3. 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 |
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 the Section called Constants of Other Types. 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 ) |
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 the Section called Subquery Expressions in Chapter 3.
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; |