(SQL Tutorials for Citizen Data Scientist)
SQL IN & BETWEEN Operators
In this tutorial you will learn how to use IN
and BETWEEN
operators with WHERE
clause.
Working with Range and Membership Conditions
In the previous chapter we’ve learned how to combine multiple conditions using the AND
and OR
operators. However, sometimes this is not sufficient and very productive, for example, if you have to check the values that lie within a range or set of values.
And here the IN
and BETWEEN
operators comes in picture that lets you define an exclusive range or a set of values rather than combining the separate conditions.
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:
column_list
FROM table_name
WHERE
column_name
IN (value1
, value1
,…);Here, column_list are the names of columns/fields like name, age, country etc. of a database table whose values you want to fetch. Well, let’s check out some examples.
Consider we’ve an employees table in our database that has following records:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+
The following SQL statement will return only those employees whose dept_id is either 1 or 3.
Example
SELECT * FROM employees
WHERE dept_id IN (1, 3);
After executing the query, you will get the result set something like this:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | +--------+--------------+------------+--------+---------+
Similarly, you can use the NOT IN
operator, which is exact opposite of the IN
. The following SQL statement will return all the employees except those whose dept_id is not 1 or 3.
Example
SELECT * FROM employees
WHERE dept_id NOT IN (1, 3);
After executing the query, this time you will get the result set something like this:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | +--------+--------------+------------+--------+---------+
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.
Example
SELECT * FROM employees
WHERE salary BETWEEN 7000 AND 9000;
After execution, you will get the output something like this:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | +--------+--------------+------------+--------+---------+
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’):
Example
SELECT * FROM employees WHERE hire_date
BETWEEN CAST('2006-01-01' AS DATE) AND CAST('2016-12-31' AS DATE);
After executing the query, you will get the result set something like this:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+
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’:
Example
SELECT * FROM employees
WHERE emp_name BETWEEN 'O' AND 'Z';
After execution, you will get the output something like this:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | +--------+--------------+------------+--------+---------+
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.