In this article, we’ll be discussing the comparison between IN and BETWEEN Operator in SQL.
However, the main difference is, BETWEEN Operator fetches tuples that lies within the specified range, whereas, IN Operator is used as an alternative to multiple ORs.
Basis | Between Operator | IN Operator |
Definition | It is used to get a set of records ranging between two values | It is used to get records whose column's value matches with given set of values |
Syntax | SELECT * FROM tableName WHERE Column BETWEEN range1 AND range2; | SELECT * FROM tableName WHERE name IN(value,value1,value2.....); |
Working | Return the records whose column's value lies in between the given range | Compares the given column's value, returns that record if a match exists in the set of values |
This Keyword is used to get a set of records which falls within a given range. In other words, It is equivalent to <= lower_bound and >=upper_bound.
It helps to shrink a long statement (1<=id AND id<=5) to Id BETWEEN 1 AND 5. We can use it with numeric values as well as alphabates.
Syntax:
SELECT * FROM tableName WHERE Column BETWEEN range1 AND range2;
For Example, there is a table coder and the table looks like:
Suppose, We require all the records whose ID ranges from 1 to 3.
SELECT * FROM coder WHERE id BETWEEN 1 AND 3;
Instead of using, multiple ORs, we have IN keyword to select only those records whose column’s value matches with given values.
Like BETWEEN keyword, It also reduces the length of statements.
Syntax:
SELECT * FROM tableName WHERE name IN(value,value1,value2…..);
Note: if it isn’t numeric then each value must be enclosed within quotes.
It would return only those records whose targeted column consists values that matches with mentioned set of values.
For Example:
This the table from which I wish to select records of ‘john’ and ‘micky’ from coder table
SELECT * FROM coder WHERE first_name IN (‘john’,’micky’);
This post was last modified on August 17, 2022