Revision 855840948 of "Where (SQL)" on enwiki{{Refimprove|date=September 2014}}
A <code>value1</code> clause in [[SQL]] specifies that a SQL [[Data Manipulation Language|Data Manipulation Language (DML)]] statement should only affect rows that meet specified criteria. The criteria are expressed in the form of predicates. <code>Value 2</code> clauses are not mandatory clauses of SQL DML statements, but can be used to limit the number of rows affected by a SQL DML statement or returned by a query. In brief SQL WHERE clause is used to extract only those results from a SQL statement, such as: SELECT, INSERT, UPDATE, or DELETE statement.<ref>{{cite web|url=http://www.programmingunit.com/2014/01/30/sql-clause-things-beginners-must-know/|title=SQL WHERE Clause – Things beginners must know}}</ref>
==Overview==
<code>WHERE</code> is an [[SQL:2003|SQL]] reserved word.
The <code>WHERE</code> clause is used in conjunction with SQL DML statements, and takes the following general form:
<source lang="sql">
SQL-DML-Statement
FROM table_name
WHERE predicate
</source>
all rows for which the predicate in the <code>WHERE</code> clause is True are affected (or returned) by the SQL DML statement or query. Rows for which the predicate evaluates to False or Unknown ([[Null (SQL)|NULL]]) are unaffected by the DML statement or query.
The following query returns only those rows from table ''mytable'' where the value in column ''mycol'' is greater than 100.
<source lang="sql">
SELECT *
FROM mytable
WHERE mycol > 100
</source>
The following [[Delete (SQL)|<code>DELETE</code> statement]] removes only those rows from table ''mytable'' where the column ''mycol'' is either NULL or has a value that is equal to 100.
<source lang="sql">
DELETE
FROM mytable
WHERE mycol IS NULL OR mycol = 100
</source>
== Predicates ==
Simple predicates use one of the operators <code>=</code>, <code><></code>, <code>></code>, <code>>=</code>, <code><</code>, <code><=</code>, <code>IN</code>, <code>BETWEEN</code>, <code>LIKE</code>, <code>IS NULL</code> or <code>IS NOT NULL</code>.
Predicates can be enclosed in parentheses if desired. The keywords <code>AND</code> and <code>OR</code> can be used to combine two predicates into a new one. If multiple combinations are applied, parentheses can be used to group combinations to indicate the order of evaluation. Without parentheses, the <code>AND</code> operator has a stronger binding than <code>OR</code>.
The following example deletes rows from ''mytable'' where the value of ''mycol'' is greater than 100, '''and''' the value of ''item'' is equal to the [[string literal]] 'Hammer':
<source lang="sql">
DELETE
FROM mytable
WHERE mycol > 100 AND item = 'Hammer'
</source>
=== IN ===
<code>IN</code> will find any values existing in a set of candidates.
<source lang="sql">
SELECT ename WHERE ename IN ('value1', 'value2', ...)
</source>
All rows match the predicate if their value is one of the candidate set of values. This is the same behavior as
<source lang="sql">
SELECT ename WHERE ename='value1' OR ename='value2'
</source>
except that the latter could allow comparison of several columns, which each <code>IN</code> clause does not. For a larger number of candidates, <code>IN</code> is less verbose.
=== BETWEEN ===
<code>BETWEEN</code> will find any values within a range.
<source lang="sql">
SELECT ename WHERE ename BETWEEN 'value1' AND 'value2'
</source>
<source lang="sql">
SELECT salary from emp WHERE salary BETWEEN 5000 AND 10000
</source>
All rows match the predicate if their value is between 'value1' and 'value2', inclusive.
=== LIKE ===
<code>LIKE</code> will find a string fitting a certain description.
* Ending [[Wildcard character|Wildcard]]
** Find any string that begins with the letter 'S'<source lang="sql">
SELECT ename FROM emp WHERE ename LIKE 'S%';</source>
* Leading Wildcard
** Find any string that ends with the letter 'S'<source lang="sql">
SELECT ename FROM emp WHERE ename LIKE '%S';</source>
* Multiple Wildcards
** Find any string that contains, anywhere, the letter 'S'<source lang="sql">
SELECT ename FROM emp WHERE ename LIKE '%S%';</source>
* Single Character Wildcard
** Find any string that contains the letter 'A' followed by any single character followed by the letter 'E'<source lang="sql">
SELECT ename FROM emp WHERE ename LIKE '%A_E%';</source>
* Character Classes<ref>''[https://technet.microsoft.com/en-us/library/ms179859.aspx Microsoft Technet]''Retrieved 21 November 2013.</ref>
** Find any string that starts with a letter or number or the symbol '_'<source lang="sql">
SELECT ename FROM emp WHERE ename LIKE '[a-zA-Z0-9_]%';</source>
SQL programmers need to be aware that the LIKE predicate typically performs a search without the normal performance benefit of indexes. Using '=', '<>', etc.. instead will increase performance. Users of the LIKE predicate should be aware that case sensitivity (e.g., 'S' versus 's') may be different based upon database product or configuration.
== References ==
<references />
== External links ==
* [http://www.psoug.org/reference/conditions.html PSOUG Home Puget Sound Oracle Users Group] gives several examples of SELECT statements with WHERE clauses.
{{SQL}}
[[Category:SQL keywords]]All content in the above text box is licensed under the Creative Commons Attribution-ShareAlike license Version 4 and was originally sourced from https://en.wikipedia.org/w/index.php?oldid=855840948.
![]() ![]() This site is not affiliated with or endorsed in any way by the Wikimedia Foundation or any of its affiliates. In fact, we fucking despise them.
|