SQL Where
The SQL WHERE keyword is used when you want to apply a conditional statement to your query. It can be used in conjunction with any SQL query.
SQL WHERE Syntax
FROM “table_name”
WHERE “condition”
By adding the SQL WHERE keyword to a SQL query will only return results that match your condition.
SQL WHERE Example
To demonstrate the above example assume that we have the following table called Games
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1001 | GTA IV | April 29, 2008 | Action-adventure | PS3,XBOX360 |
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
| 1003 | Assassin’s Creed | November 14, 2007 | Third person | PS3,XBOX360 |
| 1004 | Battlefield: Bad Company | June 26, 2008 | First person shooter | PS3,XBOX360 |
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
To SELECT a specific record in the Games table we would use the following query:
FROM Games
WHERE ID=1002
Once executed the resultset would look like this:
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
As the above example was using a numeric value there is no need to use quotes but if you wanted to perform a query where the SQL WHERE clause used a text value you would need to use quotes. Below is another example to highlight the difference.
FROM Games
WHERE Title=’LittleBigPlanet’
Once executed the resultset would look like this:
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
Give it a go and once you feel comfortable using the SQL WHERE keyword move on to our next tutorial SQL ORDER BY.