|
A <code>WHERE</code> clause in SQL specifies that a SQL 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. In brief SQL WHERE clause is used to extract only those results from a SQL statement, such as: <code>SELECT</code>, <code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code> statement. Overview <code>WHERE</code> is an SQL reserved word. The <code>WHERE</code> clause is used in conjunction with SQL DML statements, and takes the following general form: <syntaxhighlight lang="sql"> SQL-DML-Statement FROM table_name WHERE predicate </syntaxhighlight> 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) 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. <syntaxhighlight lang="sql"> SELECT * FROM mytable WHERE mycol > 100 </syntaxhighlight> The following <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. <syntaxhighlight lang="sql"> DELETE FROM mytable WHERE mycol IS NULL OR mycol = 100 </syntaxhighlight> 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': <syntaxhighlight lang="sql"> DELETE FROM mytable WHERE mycol > 100 AND item = 'Hammer' </syntaxhighlight> IN <code>IN</code> will find any values existing in a set of candidates. <syntaxhighlight lang"sql" start"1"> SELECT ename WHERE ename IN ('Montreal', 'Quebec') </syntaxhighlight> All rows match the predicate if their value is one of the candidate set of values. This is the same behavior as <syntaxhighlight lang="sql"> SELECT ename WHERE ename'value1' OR ename'value2' </syntaxhighlight> 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. <syntaxhighlight lang="sql"> SELECT ename WHERE ename BETWEEN 'value1' AND 'value2' </syntaxhighlight> <syntaxhighlight lang="sql"> SELECT salary from emp WHERE salary BETWEEN 5000 AND 10000 </syntaxhighlight> 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'<syntaxhighlight lang="sql"> SELECT ename FROM emp WHERE ename LIKE 'S%';</syntaxhighlight> * Leading wildcard ** Find any string that ends with the letter 'S'<syntaxhighlight lang="sql"> SELECT ename FROM emp WHERE ename LIKE '%S';</syntaxhighlight> * Multiple wildcards ** Find any string that contains, anywhere, the letter 'S'<syntaxhighlight lang="sql"> SELECT ename FROM emp WHERE ename LIKE '%S%';</syntaxhighlight> * Single character wildcard ** Find any string that contains the letter 'A' followed by any single character followed by the letter 'E'<syntaxhighlight lang="sql"> SELECT ename FROM emp WHERE ename LIKE '%A_E%';</syntaxhighlight> * Character classes ** Find any string that starts with a letter or number or the symbol '_'<syntaxhighlight lang="sql"> SELECT ename FROM emp WHERE ename LIKE '%';</syntaxhighlight> The LIKE predicate typically performs a search without the normal performance benefit of indexes. Using '=', '<>', etc.. instead will increase performance. Case sensitivity (e.g., 'S' versus 's') may be different based upon database product or configuration. SIMILAR TO This one is used in PostgresSQL that supports regular expressions with the following syntax:<syntaxhighlight lang="sql"> string SIMILAR TO pattern </syntaxhighlight>It works similarly to LIKE statement mentioned above.
|
|
|