SQL syntax

The syntax of the SQL programming language is defined and maintained by ISO/IEC SC 32 as part of ISO/IEC 9075. This standard is not freely available. Despite the existence of the standard, SQL code is not completely portable among different database systems without adjustments.

The syntax of the SQL programming language is defined and maintained by ISO/IEC SC 32 as part of ISO/IEC 9075. This standard is not freely available. Despite the existence of the standard, SQL code is not completely portable among different database systems without adjustments.

The SQL language is subdivided into several language elements, including:

  • Keywords are words that are defined in the SQL language. They are either reserved (e.g. SELECT, COUNT and YEAR), or non-reserved (e.g. ASC, DOMAIN and KEY). List of SQL reserved words.
  • Identifiers are names on database objects, like tables, columns and schemas. An identifier may not be equal to a reserved keyword, unless it is a delimited identifier. Delimited identifiers means identifiers enclosed in double quotation marks. They can contain characters normally not supported in SQL identifiers, and they can be identical to a reserved word, e.g. a column named YEAR is specified as "YEAR".
    • In MySQL, double quotes are string literal delimiters by default instead. Enabling the ansi_quotes SQL mode enforces the SQL standard behavior. These can also be used regardless of this mode through backticks: YEAR.
  • Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)
  • Expressions, which can produce either scalar values, or tables consisting of columns and rows of data
  • Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) (true/false/unknown) or Boolean truth values and are used to limit the effects of statements and queries, or to change program flow.
  • Queries, which retrieve the data based on specific criteria. This is an important element of SQL.
  • Statements, which may have a persistent effect on schemata and data, or may control transactions, program flow, connections, sessions, or diagnostics.
    • SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
  • Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
OperatorDescriptionExample
=Equal toAuthor = 'Alcott'
<>Not equal to (many dialects also accept !=)Dept <> 'Sales'
>Greater thanHire_Date > '2012-01-31'
<Less thanBonus < 50000.00
>=Greater than or equalDependents >= 2
<=Less than or equalRate <= 0.05
[NOT] BETWEEN [SYMMETRIC]Between an inclusive range. SYMMETRIC inverts the range bounds if the first is higher than the second.Cost BETWEEN 100.00 AND 500.00
[NOT] LIKE [ESCAPE]Begins with a character patternFull_Name LIKE 'Will%'
Contains a character patternFull_Name LIKE '%Will%'
[NOT] INEqual to one of multiple possible valuesDeptCode IN (101, 103, 209)
IS [NOT] NULLCompare to null (missing data)Address IS NOT NULL
IS [NOT] TRUE or IS [NOT] FALSEBoolean truth value testPaidVacation IS TRUE
IS NOT DISTINCT FROMIs equal to value or both are nulls (missing data)Debt IS NOT DISTINCT FROM - Receivables
ASUsed to change a column name when viewing resultsSELECT employee AS department1

Other operators have at times been suggested or implemented, such as the skyline operator (for finding only those rows that are not 'worse' than any others).

SQL has the case expression, which was introduced in SQL-92. In its most general form, which is called a "searched case" in the SQL standard:

SQL tests WHEN conditions in the order they appear in the source. If the source does not specify an ELSE expression, SQL defaults to ELSE NULL. An abbreviated syntax called "simple case" can also be used:

This syntax uses implicit equality comparisons, with the usual caveats for comparing with NULL.

There are two short forms for special CASE expressions: COALESCE and NULLIF.

The COALESCE expression returns the value of the first non-NULL operand, found by working from left to right, or NULL if all the operands equal NULL.

is equivalent to:

The NULLIF expression has two operands and returns NULL if the operands have the same value, otherwise it has the value of the first operand.

is equivalent to

Standard SQL allows two formats for comments: -- comment, which is ended by the first newline, and /* comment */, which can span multiple lines.

The most common operation in SQL, the query, makes use of the declarative SELECT statement. SELECT retrieves data from one or more tables, or expressions. Standard SELECT statements have no persistent effects on the database. Some non-standard implementations of SELECT can have persistent effects, such as the SELECT INTO syntax provided in some databases.

Queries allow the user to describe desired data, leaving the database management system (DBMS) to carry out planning, optimizing, and performing the physical operations necessary to produce that result as it chooses.

A query includes a list of columns to include in the final result, normally immediately following the SELECT keyword. An asterisk ("*"), or "wildcard", can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include:

  • The FROM clause, which indicates the table(s) to retrieve data from. The FROM clause can include optional JOIN subclauses to specify the rules for joining tables.
  • The WHERE clause includes a condition, which restricts the rows returned by the query. The WHERE clause eliminates all rows from the result set where the condition does not evaluate to True.
  • The GROUP BY clause projects rows having common values into a smaller set of rows. GROUP BY is often used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. The WHERE clause is applied before the GROUP BY clause.
  • The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY clause. Because it acts on the results of the GROUP BY clause, aggregation functions can be used in the HAVING clause predicate.
  • The ORDER BY clause identifies which column[s] to use to sort the resulting data, and in which direction to sort them (ascending or descending). Without an ORDER BY clause, the order of rows returned by an SQL query is undefined.
  • The DISTINCT keyword eliminates duplicate data.
  • The OFFSET clause specifies the number of rows to skip before starting to return data.
  • The FETCH FIRST clause specifies the number of rows to return. Some SQL databases instead have non-standard alternatives, e.g. LIMIT, TOP or ROWNUM.

The clauses of a query have a particular order of execution, which is denoted by the number on the right hand side. It is as follows: