Menu

SQL WHERE Clause – A Comprehensive Guide

Exploring SQL WHERE syntax, usage, and some real-life examples to give you a robust understanding of this essential command

Written by Jagdeesh | 3 min read

Let’s dive into the heart of SQL (Structured Query Language) with a detailed look at the SQL WHERE clause.

You will explore its syntax, usage, and some real-life examples to give you a robust understanding of this essential command.

What is the SQL WHERE Clause?

Let’s start at the very beginning. SQL is a standard language used for managing data held in a relational database management system (RDBMS) or a relational data stream management system (RDSMS).

The SQL WHERE clause is the filtering mechanism in SQL. It specifies a condition while fetching data from a single table or by joining with multiple tables. If the given condition is satisfied, only then it returns a specific value from the table. You could say the WHERE clause is the gatekeeper of data – only letting through the data you specifically request.

Syntax of SQL WHERE Clause

The syntax for the WHERE clause in SQL is

sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • SELECT column1, column2, … : This is the portion of the statement where you specify the columns you want to see in the result.

  • FROM table_name : Here, you replace ‘table_name’ with the name of the table from which you want to fetch the data.

  • WHERE condition : This is where the magic happens! Replace ‘condition’ with the condition you’re trying to meet.

Using the SQL WHERE Clause

The WHERE clause is versatile, supporting several operators to filter queries:

1) Comparison operators : such as =, >, <, >=, <=, <> or !=.

2) Logical operators : such as AND, OR, NOT.

3) IN operator : to specify multiple values in a WHERE clause.

4) BETWEEN operator : to filter values within a certain range.

5) LIKE operator: for pattern matching.

Practical Examples

Let’s use the ‘Customers’ table to illustrate

output
-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 2  | Sarah | LA   | 80      |
| 3  | Bob   | SF   | 120     |
| 4  | Alice | NYC  | 75      |
-------------------------------

Example 1: Using Comparison Operator

Suppose we want to fetch all the customers from NYC, we can use the WHERE clause as follows:

sql
SELECT * 
FROM Customers 
WHERE City = 'NYC';

This query will return

output
-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 4  | Alice | NYC  | 75      |

Example 2: Using Logical Operators

Now, let’s fetch the customers who are from either NYC or LA

sql
SELECT * 
FROM Customers 
WHERE City = 'NYC' OR City = 'LA';

The output will be

output
-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 2  | Sarah | LA   | 80      |
| 4  | Alice | NYC  | 75      |

Example 3: Using IN Operator

The IN operator allows you to specify multiple values. If you want to fetch data for John and Alice, we can do this

sql
SELECT * 
FROM Customers 
WHERE Name IN ('John', 'Alice');

This will return

output
-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 4  | Alice | NYC  | 75      |

Example 4: Using BETWEEN operator

sql
SELECT * 
FROM Orders
WHERE Amount BETWEEN 50 AND 100;

This query will output

output
-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 2  | Sarah | LA   | 80      |
| 4  | Alice | NYC  | 75      |

Example 5: Using LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specific pattern in a column.

Let’s consider our ‘Customers’ table again, and we want to find all customers whose names start with ‘A’:

sql
SELECT * 
FROM Customers 
WHERE Name LIKE 'A%';

Here, % is a wildcard character that matches any sequence of characters.

This query will output

output
-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 4  | Alice | NYC  | 75      |

Conclusion

The SQL WHERE clause is an essential part of SQL. Its versatility enables us to filter data to meet our exact needs, offering an efficient and streamlined way to interact with our databases. .

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science