Expressions

SQL92 allows expressions to transform data in tables. Expressions may contain operators (see Operators for more details) and functions (Functions has more information).

An expression is one of the following:

( a_expr )
constant
attribute
a_expr binary_operator a_expr
a_expr right_unary_operator
left_unary_operator a_expr
parameter
functional expressions
aggregate expressions

We have already discussed constants and attributes. The two kinds of operator expressions indicate respectively binary and left_unary expressions. The following sections discuss the remaining options.

Parameters

A parameter is used to indicate a parameter in a SQL function. Typically this is used in SQL function definition statement. The form of a parameter is:

$number
     

For example, consider the definition of a function, dept, as

CREATE FUNCTION dept (name)
	RETURNS dept
	AS 'select * from 
	    dept where name=$1'
	LANGUAGE 'sql';
     

Functional Expressions

A functional expression is the name of a legal SQL function, followed by its argument list enclosed in parentheses:

function (a_expr [, a_expr )
     

For example, the following computes the square root of an employee salary:

sqrt(emp.salary)
     

Aggregate Expression

An aggregate expression represents a simple aggregate (i.e., one that computes a single value) or an aggregate function (i.e., one that computes a set of values). The syntax is the following:

aggregate_name (attribute)
     
where aggregate_name must be a previously defined aggregate.

Target List

A target list is a parenthesized, comma-separated list of one or more elements, each of which must be of the form:

a_expr [ AS result_attname ]
     
where result_attname is the name of the attribute to be created (or an already existing attribute name in the case of update statements.) If result_attname is not present, then a_expr must contain only one attribute name which is assumed to be the name of the result field. In Postgres default naming is only used if a_expr is an attribute.

Qualification

A qualification consists of any number of clauses connected by the logical operators:

NOT
AND
OR

A clause is an a_expr that evaluates to a boolean over a set of instances.

From List

The from list is a comma-separated list of from expressions. Each "from expression" is of the form:

[ class_reference ] instance_variable
	{, [ class_ref ] instance_variable... }
     
where class_reference is of the form
class_name [ * ]
     
The "from expression" defines one or more instance variables to range over the class indicated in class_reference. One can also request the instance variable to range over all classes that are beneath the indicated class in the inheritance hierarchy by postpending the designator asterisk ("*").