SQL IN

The SQL IN operator is used to search from a list of values in an SQL WHERE clause. The list could be the result of an SQL SELECT query.

SQL IN Operator Syntax

SELECT ColumnName1, ColumnName2, ColumnName3, ...
FROM TableName
WHERE ColumnName IN (Value1, Value2, Value3, ...)

Or:

SELECT ColumnName1, ColumnName2, ColumnName3, ...
FROM TableName1
WHERE ColumnName IN
          (
               SELECT ColumnName 
               FROM TableName2 
               WHERE prediction
          )

SQL IN Operator Example

Table: Employees

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt Finance 62000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000
304 Tyoysww Osk Development 49000

Example 1
Select employees whose first name is Mazojys, Jssme or Tyoysww:

SELECT *
FROM Employees
WHERE FirstName IN ('Mazojys', 'Jssme', 'Tyoysww')

The result will look like:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
302 Jssme Bdnaa Development 75000
304 Tyoysww Osk Development 49000

Example 2
We have another table EmpProject identifies the employees which projects they involved in.
Table: EmpProject

EmployeeId ProjectId
302 0032
303 0032
304 0034

We want to select employees who take part in the project 0032:

SELECT *
FROM Employees
WHERE EmployeeId IN 
          (
               SELECT EmployeeId
               FROM EmpProject 
               WHERE ProjectId = '0032'
          )

The result will look like:

EmployeeId FirstName LastName Department Salary
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>