SQL IN |
SQL Sample Code - SQL IN Operator. |
| SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,value3...) |
See the SQL samples section below for an explanation of the IN operator.
What does the SQL IN operator do? |
SQL IN operator used as part of a WHERE clause works the same as the OR operator except it is easier to write.
As with the OR operator, IN uses the conditions specified in the WHERE part of the statement. The IN operator retrieves the records which matches either IN conditions in the statement.
Sample – SQL IN |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In this SQL sample we want to retrieve all the columns from the tblCompany table whose company is based in the town of Hamburg or Pisa.
Use the SELECT statement below:
| SELECT * FROM tblCompany WHERE Town IN (‘Pisa’, ’Hamburg’); |
Use the SELECT statement below:
The table of results will look like this:
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
This could have been written using the OR operator as follows;
| SELECT * FROM tblCompany WHERE Town=‘Pisa’ OR 'Hamburg'; |
Related SQL Sample Code:
|
|