The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT statement.
The SQL WHERE clause is used to extract only those records that fulfill a specified criterion. The SQL WHERE clause syntax
SELECT column_name(s) FROM table_name WHERE column_name operator value
We will use the Customers table to illustrate SQL WHERE clause usage:
| LName | FName | |
|---|---|---|
| John | Smith | John.Smith@yahoo.com |
| Steven | Goldfish | goldfish@gmail.com |
| Paula | Kari | pb@plasa.com |
| James | Smith | jim@yahoo.co.uk |
If want to select all customers from our database table, having last name ‘Smith’ need to use the following the SQL WHERE clause syntax:
SELECT * FROM Customers WHERE FName = ‘Smith’
The result of the SQL WHERE clause above will be the following:
| LName | FName | |
|---|---|---|
| John | Smith | John.Smith@yahoo.com |
| James | Smith | jim@yahoo.co.uk |
SQL uses single quotes around text values (most database systems will also accept double quotes). Although, numeric values should not be enclosed in quotes.
For text values:
This is correct:
SELECT * FROM Clients WHERE FirstName=’Alomohora’
This is wrong:
SELECT * FROM Clients WHERE FirstName=Alomohora
For numeric values:
This is correct:
SELECT * FROM Clients WHERE Year=1986
This is wrong:
SELECT * FROM Clients WHERE Year=’1986′
Related Posts: