Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between = and IN operator in SQL

  • Last Updated : 21 Aug, 2020

Prerequisite – SQL Commands
In this article we are going to see the difference between = and IN operator in SQL.

1. = Operator :
The = operator is used with Where Clause in SQL.
For Example consider the student table given below,

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

ROLL_NONAMEADDRESSPHONEAge
1RamDelhixxxxxxxxxx18
2RAMESHGURGAONxxxxxxxxxx18
3SUJITROHTAKxxxxxxxxxx20
4SURESHDelhixxxxxxxxxx18
3SUJITROHTAKxxxxxxxxxx20
2RAMESHGURGAONxxxxxxxxxx18



Query :
To fetch record of students with address as Delhi or ROHTAK.
The SQL query using = operator would be,

 SELECT * 
FROM Student 
WHERE ADDRESS='Delhi' OR ADDRESS='ROHTAK'; 

Output :

ROLL_NONAMEADDRESSPHONEAge
1RamDelhixxxxxxxxxx18
3SUJITROHTAKxxxxxxxxxx20
4SURESHDelhixxxxxxxxxx18
3SUJITROHTAKxxxxxxxxxx20

2. IN Operator :
The IN operator is used with Where Clause to test if the expression matches any value in the list of values. The advantage of using IN operator is that it avoids the use of multiple OR Operator.

Query :
To fetch record of students with address as Delhi or ROHTAK.
The SQL query using IN operator would be,

SELECT * 
FROM Student 
WHERE ADDRESS IN ('Delhi', 'ROHTAK'); 

Output :

ROLL_NONAMEADDRESSPHONEAge
1RamDelhixxxxxxxxxx18
3SUJITROHTAKxxxxxxxxxx20
4SURESHDelhixxxxxxxxxx18
3SUJITROHTAKxxxxxxxxxx20

Difference between = and IN Operator :

= OperatorIN Operator
It allows comparison of attributes with single value.It allows comparison of attributes with multiple value. For single value comparison behaves same as = .
For multiple comparison we have to use appropriate Operator in addition.(i.e JOIN, OR, AND, etc.)No additional use of Operator required.
Useful in scenarios when sub-query returns single value as result.Useful in scenarios when sub-query returns multiple values as result.
It will generate an error if you have more than one result on the subquery.It will not generate an error if you have multiple results on the subquery.
It is faster as compared to IN Operator.The IN clause is slower compared to = as it compares with multiple values until the condition satisfies.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!