Revision 493513718 of "Where (SQL)" on enwiki

{{Unreferenced|date=April 2007}}
A <code>WHERE</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>WHERE</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.

==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>

The proper syntax for writing SQL Where clause<ref>{{cite web |title=SQL Where video tutorial|url=http://www.sqlserver-training.com/sql-where-video-tutorial/}}</ref> is 

<font color="#0000ff" size="4">SELECT <font color="#ff0000" size="2">&lt;&lt;column list&gt;&gt;</font> FROM <font color="#ff0000" size="2">table</font> WHERE <font color="#ff0000" size="2">column </font><font color="#c0504d">operatorvalue</font></font>

== 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>
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  	
**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>
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.

== 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.

== References ==
<references />

{{SQL}}

{{DEFAULTSORT:Where (Sql)}}
[[Category:SQL keywords]]

[[cs:WHERE]]
[[no:Where (SQL)]]
[[ru:Where (SQL)]]
[[sq:Where (SQL)]]
[[uk:Where (SQL)]]