IN and NOT IN
Description
The IN
and NOT IN
operators in SQL are used to filter result sets based on whether a value matches any value in a specified list or subquery. These operators provide a convenient way to write conditions that check for membership within a set of values.
IN
Operator
IN
OperatorThe IN
operator is used to specify multiple values in a WHERE
clause. It checks if a value matches any value in a list or a result set from a subquery.
Syntax:
Example:
Consider a table employees
:
1
Alice
101
2
Bob
102
3
Charlie
103
4
Dave
104
5
Eve
105
To find employees in departments 101, 102, or 103:
Result:
Alice
101
Bob
102
Charlie
103
NOT IN
Operator
NOT IN
OperatorThe NOT IN
operator is used to specify multiple values in a WHERE
clause. It checks if a value does not match any value in a list or a result set from a subquery.
Syntax:
Example:
To find employees not in departments 101, 102, or 103:
Result:
Dave
104
Eve
105
Last updated
Was this helpful?