(SQL Tutorials for Citizen Data Scientist)
SQL AND & OR Operators
In this tutorial you will learn how to use the AND
& OR
operators with the WHERE
clause to filter records based on more than one condition.
Selecting Record Based on Condition
In the previous chapter we’ve learned how to fetch records from a table using a single condition with the WHERE
clause. But sometimes you need to filter records based on multiple conditions like selecting users whose ages are greater than 30 and country is United States, selecting products whose price is lower than 100 dollar and ratings is greater than 4, etc.
The AND
Operator
The AND
operator is a logical operator that combines two conditions and returns TRUE
only if both condition evaluate to TRUE
. The AND
operator is often used in the WHERE
clause of the SELECT
, UPDATE
, DELETE
statement to form conditions to filter the result set.
SELECTcolumn1_name
,column2_name
,columnN_name
FROMtable_name
WHEREcondition1
ANDcondition2
;
Let’s check out some examples that demonstrate how it actually works.
Suppose we’ve a table called employees in our database with the 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 | +--------+--------------+------------+--------+---------+
Using WHERE
Clause with AND
Operator
The following SQL statement will return only those employees from the employees table whose salary is greater than 7000 and the dept_id is equal to 5.
Example
SELECT * FROM employees
WHERE salary > 7000 AND dept_id = 5;
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 | +--------+--------------+------------+--------+---------+
The OR
Operator
Similarly, the OR
operator is also a logical operator that combines two conditions, but it returns TRUE
when either of the conditions is TRUE
.
The following SQL statement will return all the employees from the employees table whose salary is either greater than 7000 or the dept_id is equal to 5.
Example
SELECT * FROM employees
WHERE salary > 7000 OR dept_id = 5;
This time 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 | +--------+--------------+------------+--------+---------+
Combining AND
& OR
Operator
You can also combine AND
and OR
to create complex conditional expressions.
The following SQL statement will return all the employees whose salary is greater than 5000 and the dept_id is either equal to 1 or 5.
Example
SELECT * FROM employees
WHERE salary > 5000 AND (dept_id = 1 OR dept_id = 5);
After executing the above query, 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 | +--------+--------------+------------+--------+---------+
Beginners Guide to SQL – SQL AND & OR Operators
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.