Where (SQL)
This articleneeds additional citations forverification.(September 2014) |
AWHERE
clause inSQLspecifies that a SQLData Manipulation Language (DML)statement should only affectrowsthat meet specified criteria. The criteria are expressed in the form of predicates.WHERE
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
,orDELETE
statement.[1]
Overview
[edit]WHERE
is anSQLreserved word.
TheWHERE
clause is used in conjunction with SQL DML statements, and takes the following general form:
SQL-DML-Statement
FROMtable_name
WHEREpredicate
all rows for which the predicate in theWHERE
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 tablemytablewhere the value incolumnmycolis greater than 100.
SELECT*
FROMmytable
WHEREmycol>100
The followingDELETE
statementremoves only those rows from tablemytablewhere the columnmycolis either NULL or has a value that is equal to 100.
DELETE
FROMmytable
WHEREmycolISNULLORmycol=100
Predicates
[edit]Simple predicates use one of the operators=
,<>
,>
,>=
,<
,<=
,IN
,BETWEEN
,LIKE
,IS NULL
orIS NOT NULL
.
Predicates can be enclosed in parentheses if desired. The keywordsAND
andOR
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, theAND
operator has a stronger binding thanOR
.
The following example deletes rows frommytablewhere the value ofmycolis greater than 100,andthe value ofitemis equal to thestring literal'Hammer':
DELETE
FROMmytable
WHEREmycol>100ANDitem='Hammer'
IN
[edit]IN
will find any values existing in a set of candidates.
SELECTenameWHEREenameIN('Montreal','Quebec')
All rows match the predicate if their value is one of the candidate set of values. This is the same behavior as
SELECTenameWHEREename='value1'ORename='value2'
except that the latter could allow comparison of several columns, which eachIN
clause does not. For a larger number of candidates,IN
is less verbose.
BETWEEN
[edit]BETWEEN
will find any values within a range.
SELECTenameWHEREenameBETWEEN'value1'AND'value2'
SELECTsalaryfromempWHEREsalaryBETWEEN5000AND10000
All rows match the predicate if their value is between 'value1' and 'value2', inclusive.
LIKE
[edit]LIKE
will find a string fitting a certain description.
- Endingwildcard
- Find any string that begins with the letter 'S'
SELECTenameFROMempWHEREenameLIKE'S%';
- Find any string that begins with the letter 'S'
- Leading wildcard
- Find any string that ends with the letter 'S'
SELECTenameFROMempWHEREenameLIKE'%S';
- Find any string that ends with the letter 'S'
- Multiple wildcards
- Find any string that contains, anywhere, the letter 'S'
SELECTenameFROMempWHEREenameLIKE'%S%';
- Find any string that contains, anywhere, the letter 'S'
- Single character wildcard
- Find any string that contains the letter 'A' followed by any single character followed by the letter 'E'
SELECTenameFROMempWHEREenameLIKE'%A_E%';
- Find any string that contains the letter 'A' followed by any single character followed by the letter 'E'
- Character classes[2]
- Find any string that starts with a letter or number or the symbol '_'
SELECTenameFROMempWHEREenameLIKE'[a-zA-Z0-9_]%';
- Find any string that starts with a letter or number or the symbol '_'
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
[edit]This one is used inPostgresSQLthat supportsregular expressionswith the following syntax:[3]
string[NOT]SIMILARTOpattern[ESCAPEescape-character]
It works similarly to LIKE statement mentioned above.
References
[edit]- ^"SQL WHERE Clause – Things beginners must know".
- ^Microsoft TechnetRetrieved 21 November 2013.
- ^"9.7. Pattern Matching".PostgreSQL Documentation.2023-05-11.Retrieved2023-06-10.
External links
[edit]- PSOUG Home Puget Sound Oracle Users Groupgives several examples of SELECT statements with WHERE clauses.