SQL Syntax syntax SQL This chapter describes the syntax of SQL. Lexical Structure SQL input consists of a sequence of commands. A command is composed of a sequence of tokens, terminated by a semicolon (;). The end of the input stream also terminates a command. Which tokens are valid depends on the syntax of the particular command. A token can be a key word, an identifier, a quoted identifier, a literal (or constant), or a special character symbol. Tokens are normally separated by whitespace (space, tab, newline), but need not be if there is no ambiguity (which is generally only the case if a special character is adjacent to some other token type). Additionally, comments can occur in SQL input. They are not tokens, they are effectively equivalent to whitespace. For example, the following is (syntactically) valid SQL input: SELECT * FROM MY_TABLE; UPDATE MY_TABLE SET A = 5; INSERT INTO MY_TABLE VALUES (3, 'hi there'); This is a sequence of three commands, one per line (although this is not required; more than one command can be on a line, and commands can usefully be split across lines). The SQL syntax is not very consistent regarding what tokens identify commands and which are operands or parameters. The first few tokens are generally the command name, so in the above example we would usually speak of a SELECT, an UPDATE, and an INSERT command. But for instance the UPDATE command always requires a SET token to appear in a certain position, and this particular variation of INSERT also requires a VALUES in order to be complete. The precise syntax rules for each command are described in the Reference Manual. Identifiers and Key Words identifiers key words syntax Tokens such as SELECT, UPDATE, or VALUES in the example above are examples of key words, that is, words that have a fixed meaning in the SQL language. The tokens MY_TABLE and A are examples of identifiers. They identify names of tables, columns, or other database objects, depending on the command they are used in. Therefore they are sometimes simply called names. Key words and identifiers have the same lexical structure, meaning that one cannot know whether a token is an identifier or a key word without knowing the language. A complete list of key words can be found in . SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, digits (0-9), or underscores, although the SQL standard will not define a key word that contains digits or starts or ends with an underscore. The system uses no more than NAMEDATALEN-1 characters of an identifier; longer names can be written in commands, but they will be truncated. By default, NAMEDATALEN is 32 so the maximum identifier length is 31 (but at the time the system is built, NAMEDATALEN can be changed in src/include/postgres_ext.h). case sensitivity SQL commands Identifier and key word names are case insensitive. Therefore UPDATE MY_TABLE SET A = 5; can equivalently be written as uPDaTE my_TabLE SeT a = 5; A convention often used is to write key words in upper case and names in lower case, e.g., UPDATE my_table SET a = 5; quotes and identifiers There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named select, whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected. The example can be written with quoted identifiers like this: UPDATE "my_table" SET "a" = 5; Quoted identifiers can contain any character other than a double quote itself. This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies. Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it. Constants constants There are four kinds of implicitly-typed constants in PostgreSQL: strings, bit strings, integers, and floating-point numbers. Constants can also be specified with explicit types, which can enable more accurate representation and more efficient handling by the system. The implicit constants are described below; explicit constants are discussed afterwards. String Constants character strings constants quotes escaping A string constant in SQL is an arbitrary sequence of characters bounded by single quotes ('), e.g., 'This is a string'. SQL allows single quotes to be embedded in strings by typing two adjacent single quotes (e.g., 'Dianne''s horse'). In PostgreSQL single quotes may alternatively be escaped with a backslash (\, e.g., 'Dianne\'s horse'). C-style backslash escapes are also available: \b is a backspace, \f is a form feed, \n is a newline, \r is a carriage return, \t is a tab, and \xxx, where xxx is an octal number, is the character with the corresponding ASCII code. Any other character following a backslash is taken literally. Thus, to include a backslash in a string constant, type two backslashes. The character with the code zero cannot be in a string constant. Two string constants that are only separated by whitespace with at least one newline are concatenated and effectively treated as if the string had been written in one constant. For example: SELECT 'foo' 'bar'; is equivalent to SELECT 'foobar'; but SELECT 'foo' 'bar'; is not valid syntax, and PostgreSQL is consistent with SQL9x in this regard. Bit-String Constants bit strings constants Bit-string constants look like string constants with a B (upper or lower case) immediately before the opening quote (no intervening whitespace), e.g., B'1001'. The only characters allowed within bit-string constants are 0 and 1. Bit-string constants can be continued across lines in the same way as regular string constants. Integer Constants Integer constants in SQL are sequences of decimal digits (0 though 9) with no decimal point and no exponent. The range of legal values depends on which integer data type is used, but the plain integer type accepts values ranging from -2147483648 to +2147483647. (The optional plus or minus sign is actually a separate unary operator and not part of the integer constant.) Floating-Point Constants floating point constants Floating-point constants are accepted in these general forms: digits.digitse+-digits digits.digitse+-digits digitse+-digits where digits is one or more decimal digits. At least one digit must be before or after the decimal point. At least one digit must follow the exponent delimiter (e) if that field is present. Thus, a floating-point constant is distinguished from an integer constant by the presence of either the decimal point or the exponent clause (or both). There must not be a space or other characters embedded in the constant. These are some examples of valid floating-point constants: 3.5 4. .001 5e2 1.925e-3 Floating-point constants are of type DOUBLE PRECISION. REAL can be specified explicitly by using SQL string notation or PostgreSQL type notation: REAL '1.23' -- string style '1.23'::REAL -- PostgreSQL (historical) style Constants of Other Types data types constants A constant of an arbitrary type can be entered using any one of the following notations: type 'string' 'string'::type CAST ( 'string' AS type ) The string's text is passed to the input conversion routine for the type called type. The result is a constant of the indicated type. The explicit type cast may be omitted if there is no ambiguity as to the type the constant must be (for example, when it is passed as an argument to a non-overloaded function), in which case it is automatically coerced. It is also possible to specify a type coercion using a function-like syntax: typename ( 'string' ) but not all type names may be used in this way; see for details. The ::, CAST(), and function-call syntaxes can also be used to specify run-time type conversions of arbitrary expressions, as discussed in . But the form type 'string' can only be used to specify the type of a literal constant. Another restriction on type 'string' is that it does not work for array types; use :: or CAST() to specify the type of an array constant. Array constants arrays constants The general format of an array constant is the following: '{ val1 delim val2 delim ... }' where delim is the delimiter character for the type, as recorded in its pg_type entry. (For all built-in types, this is the comma character ,.) Each val is either a constant of the array element type, or a subarray. An example of an array constant is '{{1,2,3},{4,5,6},{7,8,9}}' This constant is a two-dimensional, 3-by-3 array consisting of three subarrays of integers. Individual array elements can be placed between double-quote marks (") to avoid ambiguity problems with respect to whitespace. Without quote marks, the array-value parser will skip leading whitespace. (Array constants are actually only a special case of the generic type constants discussed in the previous section. The constant is initially treated as a string and passed to the array input conversion routine. An explicit type specification might be necessary.) Operators operators syntax An operator is a sequence of up to NAMEDATALEN-1 (31 by default) characters from the following list: + - * / < > = ~ ! @ # % ^ & | ` ? $ There are a few restrictions on operator names, however: $ (dollar) cannot be a single-character operator, although it can be part of a multiple-character operator name. -- and /* cannot appear anywhere in an operator name, since they will be taken as the start of a comment. A multiple-character operator name cannot end in + or -, unless the name also contains at least one of these characters: ~ ! @ # % ^ & | ` ? $ For example, @- is an allowed operator name, but *- is not. This restriction allows PostgreSQL to parse SQL-compliant queries without requiring spaces between tokens. When working with non-SQL-standard operator names, you will usually need to separate adjacent operators with spaces to avoid ambiguity. For example, if you have defined a left unary operator named @, you cannot write X*@Y; you must write X* @Y to ensure that PostgreSQL reads it as two operator names not one. Special Characters Some characters that are not alphanumeric have a special meaning that is different from being an operator. Details on the usage can be found at the location where the respective syntax element is described. This section only exists to advise the existence and summarize the purposes of these characters. A dollar sign ($) followed by digits is used to represent the positional parameters in the body of a function definition. In other contexts the dollar sign may be part of an operator name. Parentheses (()) have their usual meaning to group expressions and enforce precedence. In some cases parentheses are required as part of the fixed syntax of a particular SQL command. Brackets ([]) are used to select the elements of an array. See for more information on arrays. Commas (,) are used in some syntactical constructs to separate the elements of a list. The semicolon (;) terminates an SQL command. It cannot appear anywhere within a command, except within a string constant or quoted identifier. The colon (:) is used to select slices from arrays. (See .) In certain SQL dialects (such as Embedded SQL), the colon is used to prefix variable names. The asterisk (*) has a special meaning when used in the SELECT command or with the COUNT aggregate function. The period (.) is used in floating-point constants, and to separate table and column names. Comments comments in SQL A comment is an arbitrary sequence of characters beginning with double dashes and extending to the end of the line, e.g.: -- This is a standard SQL92 comment Alternatively, C-style block comments can be used: /* multiline comment * with nesting: /* nested block comment */ */ where the comment begins with /* and extends to the matching occurrence of */. These block comments nest, as specified in SQL99 but unlike C, so that one can comment out larger blocks of code that may contain existing block comments. A comment is removed from the input stream before further syntax analysis and is effectively replaced by whitespace. Lexical Precedence operators precedence The precedence and associativity of the operators is hard-wired into the parser. Most operators have the same precedence and are left-associative. This may lead to non-intuitive behavior; for example the Boolean operators < and > have a different precedence than the Boolean operators <= and >=. Also, you will sometimes need to add parentheses when using combinations of binary and unary operators. For instance SELECT 5 ! - 6; will be parsed as SELECT 5 ! (- 6); because the parser has no idea -- until it is too late -- that ! is defined as a postfix operator, not an infix one. To get the desired behavior in this case, you must write SELECT (5 !) - 6; This is the price one pays for extensibility. Operator Precedence (decreasing) Operator/Element Associativity Description . left table/column name separator :: left PostgreSQL-style typecast [ ] left array element selection - right unary minus ^ left exponentiation * / % left multiplication, division, modulo + - left addition, subtraction IS test for TRUE, FALSE, UNKNOWN, NULL ISNULL test for NULL NOTNULL test for NOT NULL (any other) left all other native and user-defined operators IN set membership BETWEEN containment OVERLAPS time interval overlap LIKE ILIKE string pattern matching < > less than, greater than = right equality, assignment NOT right logical negation AND left logical conjunction OR left logical disjunction
Note that the operator precedence rules also apply to user-defined operators that have the same names as the built-in operators mentioned above. For example, if you define a + operator for some custom data type it will have the same precedence as the built-in + operator, no matter what yours does.
Schemas and naming conventions schemas search path namespaces A PostgreSQL database cluster (installation) contains one or more named databases. Users and groups of users are shared across the entire cluster, but no other data is shared across databases. Any given client connection to the server can access only the data in a single database, the one specified in the connection request. Users of a cluster do not necessarily have the privilege to access every database in the cluster. Sharing of user names means that there cannot be different users named, say, joe in two databases in the same cluster; but the system can be configured to allow joe access to only some of the databases. A database contains one or more named schemas, which in turn contain tables. Schemas also contain other kinds of named objects, including datatypes, functions, and operators. The same object name can be used in different schemas without conflict; for example, both schema1 and myschema may contain tables named mytable. Unlike databases, schemas are not rigidly separated: a user may access objects in any of the schemas in the database he is connected to, if he has privileges to do so. qualified names names qualified To name a table precisely, write a qualified name consisting of the schema name and table name separated by a dot: schema.table Actually, the even more general syntax database.schema.table can be used too, but at present this is just for pro-forma compliance with the SQL standard; if you write a database name it must be the same as the database you are connected to. unqualified names names unqualified Qualified names are tedious to write, and it's often best not to wire a particular schema name into applications anyway. Therefore tables are often referred to by unqualified names, which consist of just the table name. The system determines which table is meant by following a search path, which is a list of schemas to look in. The first matching table in the search path is taken to be the one wanted. If there is no match in the search path, an error is reported, even if matching table names exist in other schemas in the database. The first schema named in the search path is called the current schema. Aside from being the first schema searched, it is also the schema in which new tables will be created if the CREATE TABLE command does not specify a schema name. The search path works in the same way for datatype names, function names, and operator names as it does for table names. Datatype and function names can be qualified in exactly the same way as table names. If you need to write a qualified operator name in an expression, there is a special provision: you must write OPERATOR(schema.operator) This is needed to avoid syntactic ambiguity. An example is SELECT 3 OPERATOR(pg_catalog.+) 4; In practice one usually relies on the search path for operators, so as not to have to write anything so ugly as that. The standard search path in PostgreSQL contains first the schema having the same name as the session user (if it exists), and second the schema named public (if it exists, which it does by default). This arrangement allows a flexible combination of private and shared tables. If no per-user schemas are created then all user tables will exist in the shared public schema, providing behavior that is backwards-compatible with pre-7.3 PostgreSQL releases. There is no concept of a public schema in the SQL standard. To achieve closest conformance to the standard, the DBA should create per-user schemas for every user, and not use (perhaps even remove) the public schema. In addition to public and user-created schemas, each database contains a pg_catalog schema, which contains the system tables and all the built-in datatypes, functions, and operators. pg_catalog is always effectively part of the search path. If it is not named explicitly in the path then it is implicitly searched before searching the path's schemas. This ensures that built-in names will always be findable. However, you may explicitly place pg_catalog at the end of your search path if you prefer to have user-defined names override built-in names. Reserved names reserved names names reserved There are several restrictions on the names that can be chosen for user-defined database objects. These restrictions vary depending on the kind of object. (Note that these restrictions are separate from whether the name is a key word or not; quoting a name will not allow you to escape these restrictions.) Schema names beginning with pg_ are reserved for system purposes and may not be created by users. In PostgreSQL versions before 7.3, table names beginning with pg_ were reserved. This is no longer true: you may create such a table name if you wish, in any non-system schema. However, it's best to continue to avoid such names, to ensure that you won't suffer a conflict if some future version defines a system catalog named the same as your table. (With the default search path, an unqualified reference to your table name would be resolved as the system catalog instead.) System catalogs will continue to follow the convention of having names beginning with pg_, so that they will not conflict with unqualified user-table names so long as users avoid the pg_ prefix. Every table has several system columns that are implicitly defined by the system. Therefore, these names cannot be used as names of user-defined columns: columns system columns oid OID The object identifier (object ID) of a row. This is a serial number that is automatically added by PostgreSQL to all table rows (unless the table was created WITHOUT OIDS, in which case this column is not present). See for more info. tableoid The OID of the table containing this row. This attribute is particularly handy for queries that select from inheritance hierarchies, since without it, it's difficult to tell which individual table a row came from. The tableoid can be joined against the oid column of pg_class to obtain the table name. xmin The identity (transaction ID) of the inserting transaction for this tuple. (Note: A tuple is an individual state of a row; each update of a row creates a new tuple for the same logical row.) cmin The command identifier (starting at zero) within the inserting transaction. xmax The identity (transaction ID) of the deleting transaction, or zero for an undeleted tuple. It is possible for this field to be nonzero in a visible tuple: that usually indicates that the deleting transaction hasn't committed yet, or that an attempted deletion was rolled back. cmax The command identifier within the deleting transaction, or zero. ctid The physical location of the tuple within its table. Note that although the ctid can be used to locate the tuple very quickly, a row's ctid will change each time it is updated or moved by VACUUM FULL. Therefore ctid is useless as a long-term row identifier. The OID, or even better a user-defined serial number, should be used to identify logical rows. Value Expressions 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 . 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 . An example is the IS NULL clause. We have already discussed constants in . The following sections discuss the remaining options. Column References A column can be referenced in the form: correlation.columnname `['subscript`]' correlation is the name of a table (possibly qualified), or 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 for more about arrays.) Positional Parameters 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; Here the $1 will be replaced by the first function argument when the function is invoked. Operator Invocations 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) where the operator token follows the syntax rules of , or is one of the keywords AND, OR, and NOT, or is a qualified operator name OPERATOR(schema.operatorname) Which particular operators exist and whether they are unary or binary depends on what operators have been defined by the system or the user. describes the built-in operators. Function Calls The syntax for a function call is the name of a function (possibly qualified with a schema name), 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 . Other functions may be added by the user. Aggregate Expressions aggregate functions 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 ( * ) where aggregate_name is a previously defined aggregate (possibly a qualified name), and expression is any value expression that does not itself contain an aggregate expression. 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 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 . Other aggregate functions may be added by the user. Type Casts data types type casts 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 . 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 usually 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. However, automatic casting is only done for cast functions that are marked okay to apply implicitly in the system catalogs. Other cast functions must be invoked with explicit casting syntax. This restriction is intended to prevent surprising conversions from being applied silently. 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 syntactic conflicts. Therefore, the use of the function-like cast syntax leads to inconsistencies and should probably be avoided in new applications. Scalar Subqueries 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 . 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; Expression Evaluation The order of evaluation of subexpressions is not defined. In particular, the inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order. Furthermore, if the result of an expression can be determined by evaluating only some parts of it, then other subexpressions might not be evaluated at all. For instance, if one wrote SELECT true OR somefunc(); then somefunc() would (probably) not be called at all. The same would be the case if one wrote SELECT somefunc() OR true; Note that this is not the same as the left-to-right short-circuiting of Boolean operators that is found in some programming languages. As a consequence, it is unwise to use functions with side effects as part of complex expressions. It is particularly dangerous to rely on side effects or evaluation order in WHERE and HAVING clauses, since those clauses are extensively reprocessed as part of developing an execution plan. Boolean expressions (AND/OR/NOT combinations) in those clauses may be reorganized in any manner allowed by the laws of Boolean algebra. When it is essential to force evaluation order, a CASE construct may be used. For example, this is an untrustworthy way of trying to avoid division by zero in a WHERE clause: SELECT ... WHERE x <> 0 AND y/x > 1.5; but this is safe: SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END; A CASE construct used in this fashion will defeat optimization attempts, so it should only be done when necessary.