SQL syntax
{{Short description|Set of rules defining correctly structured programs}}
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.
Language elements
{{Image frame|content=
\left. \begin{array}{rl} \scriptstyle\mathtt{UPDATE~clause} & \{ \mathtt{UPDATE\ country} \\
\scriptstyle\mathtt{SET~clause} & \{ \mathtt{SET\ population =~} \overbrace\mathtt{population + 1}^\mathtt{expression} \\
\scriptstyle\mathtt{WHERE~clause} & \{ \mathtt{WHERE\ \underbrace{{name =} \overbrace{'USA'}^{expression}}_{predicate};}
\end{array}
\right\}{\scriptstyle\texttt{statement}}
|width=500|caption=A chart showing several of the SQL language elements that compose a single statement. This adds one to the population of the USA in the country table.}}
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. {{code|SELECT}}, {{code|COUNT}} and {{code|YEAR}}), or non-reserved (e.g. {{code|ASC}}, {{code|DOMAIN}} and {{code|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 {{code|YEAR}} is specified as {{code|"YEAR"}}.
- In MySQL, double quotes are string literal delimiters by default instead. Enabling the {{code|ansi_quotes}} SQL mode enforces the SQL standard behavior. These can also be used regardless of this mode through backticks: {{code|`YEAR`}}.
- Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)ANSI/ISO/IEC International Standard (IS). Database Language SQL—Part 2: Foundation (SQL/Foundation). 1999.
- 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.
Operators
class="wikitable" |
Operator
! Description ! Example |
---|
=
| Equal to | {{code|2=sql|1= Author = 'Alcott'}} |
<>
| Not equal to (many dialects also accept | {{code|2=sql|1=Dept <> 'Sales'}} |
>
| Greater than | {{code|2=sql|1=Hire_Date > '2012-01-31'}} |
<
| Less than | {{code|2=sql|1=Bonus < 50000.00}} |
>=
| Greater than or equal | {{code|2=sql|1=Dependents >= 2}} |
<=
| Less than or equal | {{code|2=sql|1=Rate <= 0.05}} |
Where (SQL)#BETWEEN
| Between an inclusive range. SYMMETRIC inverts the range bounds if the first is higher than the second. | {{code|2=sql|1=Cost BETWEEN 100.00 AND 500.00}} |
rowspan=2| Like (SQL)
| Begins with a character pattern | {{code|2=sql|1=Full_Name LIKE 'Will%'}} |
Contains a character pattern
| {{code|2=sql|1=Full_Name LIKE '%Will%'}} |
Where (SQL)#IN
| Equal to one of multiple possible values | {{code|2=sql|1=DeptCode IN (101, 103, 209)}} |
SQL syntax#Null or three-valued logic (3VL)
| Compare to null (missing data) | {{code|2=sql|1=Address IS NOT NULL}} |
{{code|2=sql|1=IS [NOT] TRUE}} or {{code|2=sql|1=IS [NOT] FALSE}}
| Boolean truth value test | {{code|2=sql|1=PaidVacation IS TRUE}} |
SQL syntax#Null or three-valued logic (3VL)
| Is equal to value or both are nulls (missing data) | {{code|2=sql|1=Debt IS NOT DISTINCT FROM - Receivables}} |
{{code|2=sql|1=AS}}
| Used to change a column name when viewing results | {{code|2=sql|1=SELECT 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).
{{anchor|Conditional (CASE) expressions}}
SQL has the {{code|case}} expression, which was introduced in SQL-92. In its most general form, which is called a "searched case" in the SQL standard:
CASE WHEN n > 0
THEN 'positive'
WHEN n < 0
THEN 'negative'
ELSE 'zero'
END
SQL tests {{code|WHEN}} conditions in the order they appear in the source. If the source does not specify an {{code|ELSE}} expression, SQL defaults to {{code|ELSE NULL}}. An abbreviated syntax called "simple case" can also be used:
CASE n WHEN 1
THEN 'One'
WHEN 2
THEN 'Two'
ELSE 'I cannot count that high'
END
This syntax uses implicit equality comparisons, with the usual caveats for comparing with NULL.
There are two short forms for special {{code|CASE}} expressions: {{code|COALESCE}} and {{code|NULLIF}}.
The {{code|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.
COALESCE(x1,x2)
is equivalent to:
CASE WHEN x1 IS NOT NULL THEN x1
ELSE x2
END
The {{code|NULLIF}} expression has two operands and returns NULL if the operands have the same value, otherwise it has the value of the first operand.
NULLIF(x1, x2)
is equivalent to
CASE WHEN x1 = x2 THEN NULL ELSE x1 END
Comments
Queries
The most common operation in SQL, the query, makes use of the declarative SELECT
statement. {{code|SELECT}} retrieves data from one or more tables, or expressions. Standard {{code|SELECT}} statements have no persistent effects on the database. Some non-standard implementations of {{code|SELECT}} can have persistent effects, such as the {{code|SELECT INTO}} syntax provided in some databases.{{cite web|url=https://learn.microsoft.com/en-us/sql/t-sql/queries/select-into-clause-transact-sql|title=SELECT - INTO Clause (Transact-SQL)|website=learn.microsoft.com|date=2023-05-23|access-date=2024-11-07}}
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 {{code|SELECT}} keyword. An asterisk ("*
") can be used to specify that the query should return all columns of the queried tables. {{code|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 {{code|FROM}} clause can include optionalJOIN
subclauses to specify the rules for joining tables. - The
WHERE
clause includes a comparison predicate, which restricts the rows returned by the query. The {{code|WHERE}} clause eliminates all rows from the result set where the comparison predicate does not evaluate to True. - The
GROUP BY
clause projects rows having common values into a smaller set of rows.{{Clarify|reason=|date=September 2020}} {{code|GROUP BY}} is often used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. The {{code|WHERE}} clause is applied before the {{code|GROUP BY}} clause. - The
HAVING
clause includes a predicate used to filter rows resulting from the {{code|GROUP BY}} clause. Because it acts on the results of the {{code|GROUP BY}} clause, aggregation functions can be used in the {{code|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 {{code|ORDER BY}} clause, the order of rows returned by an SQL query is undefined. - The
DISTINCT
keyword
{{cite book
| title = SAS 9.4 SQL Procedure User's Guide
| url = https://books.google.com/books?id=ESjMAAAAQBAJ
| publisher = SAS Institute
| date = 2013
| page = 248
| isbn = 9781612905686
| access-date = 2015-10-21
| quote = Although the UNIQUE argument is identical to DISTINCT, it is not an ANSI standard.
}}
{{cite book
| last1 = Leon
| first1 = Alexis
| author-link1 = Alexis Leon
| last2 = Leon
| first2 = Mathews
| year = 1999
| chapter = Eliminating duplicates - SELECT using DISTINCT
| title = SQL: A Complete Reference
| url = https://books.google.com/books?id=dmiPz2MMpfwC
| location = New Delhi
| publisher = Tata McGraw-Hill Education
| publication-date = 2008
| page = 143
| isbn = 9780074637081
| access-date = 2015-10-21
| quote = [...] the keyword DISTINCT [...] eliminates the duplicates from the result set.
}}
- The {{code|OFFSET}} clause specifies the number of rows to skip before starting to return data.
- The {{code|FETCH FIRST}} clause specifies the number of rows to return. Some SQL databases instead have non-standard alternatives, e.g. {{code|LIMIT}}, {{code|TOP}} or {{code|ROWNUM}}.
The clauses of a query have a particular order of execution,{{Cite web|url=https://www.designcise.com/web/tutorial/what-is-the-order-of-execution-of-an-sql-query|title=What Is The Order Of Execution Of An SQL Query? - Designcise.com|website=www.designcise.com|date=29 June 2015|access-date=2018-02-04}} which is denoted by the number on the right hand side. It is as follows:
SELECT | 5. | |||||||||||||||||||||||||||||||||||||
FROM |