SQL IN & BETWEEN Operators

The IN Operator

The IN operator is logical operator that is used to check whether a particular value exists within a set of values or not. Its basic syntax can be given with:

SELECT column_list FROM table_name
WHERE column_name IN (value1, value1,...);

The following SQL statement will return only those employees whose dept_id is either 1 or 3.

SELECT * FROM employees
WHERE dept_id IN (1, 3);

The BETWEEN Operator

Sometimes you want to select a row if the value in a column falls within a certain range. This type of condition is common when working with numeric data.

To perform the query based on such condition you can utilize the BETWEEN operator. It is a logical operator that allows you to specify a range to test, as follow:

SELECT column1_name, column2_name, columnN_name
FROM table_name
WHERE column_name BETWEEN min_value AND max_value;

Let's build and perform the queries based upon range conditions on our employees table.

Define Numeric Ranges

The following SQL statement will return only those employees from the employees table, whose salary falls within the range of 7000 and 9000.

SELECT * FROM employees 
WHERE salary BETWEEN 7000 AND 9000;

Define Date Ranges

When using the BETWEEN operator with date or time values, use the CAST() function to explicitly convert the values to the desired data type for best results. For example, if you use a string such as '2016-12-31' in a comparison to a DATE, cast the string to a DATE, as follow:

The following SQL statement selects all the employees who hired between 1st January 2006 (i.e. '2006-01-01') and 31st December 2016 (i.e. '2016-12-31'):

SELECT * FROM employees WHERE hire_date
BETWEEN CAST('2006-01-01' AS DATE) AND CAST('2016-12-31' AS DATE);

Define String Ranges

While ranges of dates and numbers are most common, you can also build conditions that search for ranges of strings. The following SQL statement selects all the employees whose name beginning with any of the letter between 'O' and 'Z':

SELECT * FROM employees
WHERE emp_name BETWEEN 'O' AND 'Z';