SQL WERE Statement

The SQL WHERE statement involves using the WHERE clause with the SELECT statement to filter out specific data from a database.

SQL WHERE statement

SELECT column_name(s)

FROM table_name

WHERE column_name operator value


Database Table “Members”

Id FirstName LastName Email City Phone
1 Gilbert Duncan [email protected] Newark 973-600-3300
2 Elvis Daniels [email protected] Piscataway 862-600-3050
3 Michael Green [email protected] Detroit 732-500-5050
4 Kelvin Graham [email protected] Detroit 732-555-5050

SQL WHERE Example

SELECT *

FROM Members

WHERE City=’Detroit’

RESULT of SQL WHERE statement above will be

Id FirstName LastName Email City Phone
3 Michael Green [email protected] Detroit 732-500-5050
4 Kelvin Graham [email protected] Detroit 732-555-5050

We can also select specific columns like example below:

SELECT FirstName, LastName, Email, City, PhoneFROM Members

WHERE LastName=’Duncan’

RESULT of SQL WHERE statement above will be

FirstName LastName Email City Phone
Gilbert Duncan [email protected] Newark 973-600-3300

Text Field Quotes

When using the SQL WHERE statement, it is very important to use the correct quotes for the text field values.

When the text field value is a text, we use the single quote

SELECT *

FROM Members

WHERE City=’Newark’

When the text field value is a number, we do not use any quote

SELECT *

FROM Members

WHERE Phone=’973-600-3300′


Operators

The SQL WHERE statement allows the following operators

Operator Description
= Equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
BETWEEN Between an inclusive range
LIKE Search for similar pattern
IN To search for records with specific value from column(s)

Important: Some versions of SQL use the != operator in place for the <> operator