Beginners Guide to SQL – SQL IN & BETWEEN Operators

(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:

SELECT column_list FROM table_name
WHERE column_name IN (value1value1,…);

Here, column_list are the names of columns/fields like nameagecountry 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 |
+--------+--------------+------------+--------+---------+

 

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

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

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!