016. Using In ()

 

The “In” keyword can be used in place of the equal sign (=), in the “Where” clause.
This is when you want to get a list of thing. Or exclude a list of things.

 

An example of In, in code:

 

–Create test data:
Create Table Table1 (RelationID Int Not Null, Relation Nvarchar(50) Not Null)
Insert Into Table1 (RelationID, Relation) Values(1, ‘Brother’)
Insert Into Table1 (RelationID, Relation) Values(2, ‘Sister’)
Insert Into Table1 (RelationID, Relation) Values(3, ‘Mother’)
Insert Into Table1 (RelationID, Relation) Values(4, ‘Grandfather’)
Insert Into Table1 (RelationID, Relation) Values(5, ‘Father’)
Create Table Table2 (RelationID Int Not Null, Relation Nvarchar(50) Not Null)
Insert Into Table2 (RelationID, Relation) Values(1, ‘Uncle’)
Insert Into Table2 (RelationID, Relation) Values(2, ‘Grand-Uncle’)
Insert Into Table2 (RelationID, Relation) Values(3, ‘Aunt’)
Insert Into Table2 (RelationID, Relation) Values(4, ‘Grand-Aunt’)
Insert Into Table2 (RelationID, Relation) Values(5, ‘Cousin’)

 

— Example of list to include:
Select
*
From
Table1 T1
Inner Join Table2 T2 On T1.RelationID = T2.RelationID
Where
T1.Relation In (‘Brother’, ‘Mother’, ‘Father’)

 

— Example of list excluded:
Select
*
From
Table1 T1
Inner Join Table2 T2 On T1.RelationID = T2.RelationID
Where
T1.Relation Not In (‘Uncle’, ‘Aunt’, ‘Cousin’)