IN, BETWEEN & IS NULL
IN, BETWEEN and IS NULL are commonly used SQL operators for filtering and checking data in SQL Server.
These operators make it easier to write conditions when working with multiple values, ranges and NULL values.
IN Operator
The IN operator is used to check whether a value matches any value from a specified list.
It is useful when we want to check multiple possible values without writing several OR conditions.
IN Syntax
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, value3);
Example Table
| StudentId | StudentName | Age | City | Fees |
|---|---|---|---|---|
| 101 | Rahul | 22 | Pune | 35000 |
| 102 | Priya | 23 | Mumbai | 40000 |
| 103 | Amit | 21 | Nashik | 30000 |
| 104 | Neha | 24 | Pune | 45000 |
Example – IN
Suppose we want students from Pune, Mumbai or Nashik.
SELECT *
FROM Students
WHERE City IN ('Pune', 'Mumbai', 'Nashik');
This returns records where City matches any of the specified values.
IN vs OR
The following two queries can be used to check the same set of city values.
SELECT *
FROM Students
WHERE City = 'Pune'
OR City = 'Mumbai'
OR City = 'Nashik';
Using IN can make this type of condition shorter and easier to read.
NOT IN
The NOT IN operator returns records whose value does not match any value in the specified list.
SELECT *
FROM Students
WHERE City NOT IN ('Pune', 'Mumbai');
This excludes students from Pune and Mumbai.
BETWEEN Operator
The BETWEEN operator is used to select values within a specified range.
BETWEEN includes both the starting and ending values.
BETWEEN Syntax
SELECT column_name
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
BETWEEN with Numbers
Suppose we want students whose age is between 21 and 23.
SELECT *
FROM Students
WHERE Age BETWEEN 21 AND 23;
This includes students with ages 21, 22 and 23.
BETWEEN with Fees
SELECT *
FROM Students
WHERE Fees BETWEEN 30000 AND 40000;
This returns students whose fees are between 30000 and 40000, including both boundary values.
NOT BETWEEN
The NOT BETWEEN operator returns values outside the specified range.
SELECT *
FROM Students
WHERE Age NOT BETWEEN 21 AND 23;
This returns students whose age is outside the range 21 to 23.
IS NULL
NULL represents a missing, unknown or unavailable value.
To check whether a column contains NULL, we use IS NULL.
SELECT *
FROM Students
WHERE City IS NULL;
This returns records where City does not contain a value.
IS NOT NULL
The IS NOT NULL condition returns records where a column contains a value.
SELECT *
FROM Students
WHERE City IS NOT NULL;
This returns records where City has a value.
Important About NULL
- NULL means the value is missing or unknown.
- NULL is not the same as zero.
- NULL is not the same as an empty string.
- Use IS NULL to check for NULL values.
- Use IS NOT NULL to check for non-NULL values.
Combining IN and BETWEEN
IN and BETWEEN can be combined with other conditions using AND or OR.
SELECT *
FROM Students
WHERE City IN ('Pune', 'Mumbai')
AND Age BETWEEN 21 AND 24;
This returns students from Pune or Mumbai whose age is between 21 and 24.
IN vs BETWEEN vs IS NULL
| Operator | Purpose | Example |
|---|---|---|
| IN | Checks multiple specific values. | City IN ('Pune', 'Mumbai') |
| BETWEEN | Checks values within a range. | Age BETWEEN 21 AND 24 |
| IS NULL | Checks for missing NULL values. | City IS NULL |
| IS NOT NULL | Checks for values that are not NULL. | City IS NOT NULL |
Important Points
- IN checks a value against multiple values.
- NOT IN excludes specified values.
- BETWEEN checks a range and includes both boundary values.
- NOT BETWEEN checks values outside a range.
- IS NULL checks for NULL values.
- IS NOT NULL checks for non-NULL values.
Summary
In this section, we learned the IN, NOT IN, BETWEEN, NOT BETWEEN, IS NULL and IS NOT NULL operators. These operators are useful for filtering SQL Server data using multiple values, ranges and NULL checks.