(SQL Tutorials for Citizen Data Scientist)
SQL WHERE Clause
In this tutorial you will learn how to select specific records from a table using SQL.
Selecting Record Based on Condition
In the previous chapter we’ve learnt how to fetch all the records from a table or table columns. But, in real world scenario we generally need to select, update or delete only those records which fulfill certain condition like users who belongs to a certain age group, or country, etc.
The WHERE
clause is used with the SELECT
, UPDATE
, and DELETE
. However, you’ll see the use of this clause with other statements in upcoming chapters.
Syntax
The WHERE
clause is used with the SELECT
statement to extract only those records that fulfill specified conditions. The basic syntax can be given with:
column_list
FROM table_name
WHERE condition
;Here, column_list are the names of columns/fields like name, age, country etc. of a database table whose values you want to fetch. However, if you want to fetch the values of all the columns available in a table, you can use the following syntax:
table_name
WHERE condition
;Now, 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 | +--------+--------------+------------+--------+---------+
Filter Records with WHERE Clause
The following SQL statement will returns all the employees from the employees table, whose salary is greater than 7000. The WHERE
clause simply filtered out the unwanted data.
Example
SELECT * FROM employees
WHERE salary > 7000;
After execution, the output will look 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 | +--------+--------------+------------+--------+---------+
As you can see the output contains only those employees whose salary is greater than 7000. Similarly, you can fetch records from specific columns, like this:
Example
SELECT emp_id, emp_name, hire_date, salary
FROM employees
WHERE salary > 7000;
After executing the above statement, you’ll get the output something like this:
+--------+--------------+------------+--------+ | emp_id | emp_name | hire_date | salary | +--------+--------------+------------+--------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | | 4 | Rick Deckard | 2007-01-03 | 7200 | +--------+--------------+------------+--------+
The following statement will fetch the records of an employee whose employee id is 2.
Example
SELECT * FROM employees
WHERE emp_id = 2;
This statement will produce the following output:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | +--------+--------------+------------+--------+---------+
This time we got only one row in the output, because emp_id is unique for every employee.
Operators Allowed in WHERE
Clause
SQL supports a number of different operators that can be used in WHERE
clause, the most important ones are summarized in the following table.
Operator | Description | Example |
---|---|---|
= |
Equal | WHERE id = 2 |
> |
Greater than | WHERE age > 30 |
< |
Less than | WHERE age < 18 |
>= |
Greater than or equal | WHERE rating >= 4 |
<= |
Less than or equal | WHERE price <= 100 |
LIKE |
Simple pattern matching | WHERE name LIKE 'Dav' |
IN |
Check whether a specified value matches any value in a list or subquery | WHERE country IN ('USA', 'UK') |
BETWEEN |
Check whether a specified value is within a range of values | WHERE rating BETWEEN 3 AND 5 |
Beginners Guide to SQL – SQL WHERE Clause
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.