Beginners Guide to SQL – SQL WHERE Clause

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

SELECT column_list FROM table_name WHERE condition;

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

SELECT * FROM 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

 

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!