SQL WHERE Clause

The WHERE clause is used to specify the selection criteria.

WHERE clause

To select data from a table conditionally, you can add a WHERE clause to the SELECT statement.

Syntax

SELECT column_name FROM table_name WHERE column operator value

The following operators can be used in the WHERE clause:

Operator Description
= Equal to
<> Not Equal to
> Greater Than
< Less Than
>= Greater Than or Equal to
<= Less Than or Equal to
BETWEEN Within a Range
LIKE Search for a Pattern

Note:In some versions of SQL, the operator <> can be written as !=.

Using WHERE Clause

If we only want to select people living in the city "Beijing", we need to add a WHERE clause to the SELECT statement:

SELECT * FROM Persons WHERE City='Beijing'

"Persons" Table

LastName FirstName Address City Year
Adams John Oxford Street London 1970
Bush George Fifth Avenue New York 1975
Carter Thomas Changan Street Beijing 1980
Gates Bill Xuanwumen 10 Beijing 1985

Result:

LastName FirstName Address City Year
Carter Thomas Changan Street Beijing 1980
Gates Bill Xuanwumen 10 Beijing 1985

Quotation Usage

Please note that we use single quotes around the condition values in our examples.

SQL uses single quotes to encloseText Value(Most database systems also accept double quotes). If it isNumeric, do not use quotes.

Text Value:

This is correct:
SELECT * FROM Persons WHERE FirstName='Bush'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Bush

Numeric Value:

This is correct:
SELECT * FROM Persons WHERE Year>1965
This is wrong:
SELECT * FROM Persons WHERE Year>'1965'