SQL IN Operator
The SQL IN OPERATOR is used when you need your condition to return matches from within a range of values.
SQL IN OPERATOR Syntax
SELECT “column_name(s)”
FROM “table_name”
WHERE “column_name” IN (‘value1′, ‘value2′, ‘value3′)
FROM “table_name”
WHERE “column_name” IN (‘value1′, ‘value2′, ‘value3′)
Now you know the syntax let me show you an example.
SQL IN Operator 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 |
SELECT *
FROM Games
WHERE ID IN (’1003′, ’1005′, ’1002′)
FROM Games
WHERE ID IN (’1003′, ’1005′, ’1002′)
Once executed the resultset would look like this:
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1003 | Assassin’s Creed | November 14, 2007 | Third person | PS3,XBOX360 |
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
Give it a go and once you feel comfortable using the SQL IN OPERATOR move on to our next tutorial SQL BETWEEN OPERATOR.