2๏ธโƒฃ ๐Ÿ”Ž MySQL SQL Fundamentals
Estimated reading: 4 minutes 273 views

MySQL WHERE Clause โ€“ Syntax, Filters, Operators & Examples


Introduction โ€“ Why Learn the WHERE Clause?

The WHERE clause is a vital part of the SQL language, especially in MySQL. It allows you to filter records from a table based on specified conditions. Whether you’re pulling customer data from a specific country or getting sales data from a particular date range, the WHERE clause is essential for targeted data retrieval.

In this tutorial, youโ€™ll learn:

  • Syntax and structure of the WHERE clause in MySQL
  • How to use comparison and logical operators
  • Advanced filtering using IN, BETWEEN, LIKE, and IS NULL
  • Real-world examples of filtering data

What is the WHERE Clause in MySQL?

The WHERE clause is used to specify conditions for filtering records in SELECT, UPDATE, DELETE, and INSERT statements.

Basic Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Examples of MySQL WHERE Clause

Example 1: Filter by Exact Match

SELECT * FROM customers
WHERE country = 'India';

Returns only customers from India.


Example 2: Filter with Numeric Condition

SELECT * FROM products
WHERE price > 500;

Shows products priced above 500.


Example 3: Filter Multiple Conditions with AND

SELECT * FROM orders
WHERE status = 'shipped' AND payment_method = 'credit card';

Returns orders that are shipped and paid by credit card.


Example 4: Filter Using OR

SELECT * FROM users
WHERE city = 'Mumbai' OR city = 'Delhi';

Retrieves users from either Mumbai or Delhi.


Example 5: Use of NOT Operator

SELECT * FROM employees
WHERE NOT department = 'HR';

Excludes records where the department is HR.


Advanced Filtering Techniques

Using BETWEEN for Range Queries

SELECT * FROM transactions
WHERE amount BETWEEN 1000 AND 5000;

Fetches records where the amount is within the given range.


Using IN for Multiple Values

SELECT * FROM orders
WHERE status IN ('pending', 'shipped', 'delivered');

Matches any of the listed statuses.


Using LIKE for Pattern Matching

SELECT * FROM customers
WHERE name LIKE 'A%';

Returns customers whose names start with “A”.

SELECT * FROM customers
WHERE email LIKE '%@gmail.com';

Returns customers with Gmail addresses.


Using IS NULL or IS NOT NULL

SELECT * FROM payments
WHERE discount IS NULL;

Filters rows where the discount column has no value.


WHERE Clause Operators Summary

OperatorDescriptionExample
=Equal toWHERE age = 30
!= / <>Not equal toWHERE status != 'inactive'
> / <Greater/Less thanWHERE salary > 50000
>= / <=Greater/Less than or equal toWHERE marks >= 60
BETWEENRange (inclusive)WHERE age BETWEEN 20 AND 30
INMatch any value in a listWHERE city IN ('Delhi', 'Pune')
LIKEPattern matchingWHERE name LIKE 'S%'
IS NULLCheck for NULLWHERE address IS NULL

WHERE Clause Use Cases in MySQL

ScenarioSQL Example
Find customers in a citySELECT * FROM customers WHERE city = 'Bangalore';
๐Ÿ›๏ธ Get orders above โ‚น10,000SELECT * FROM orders WHERE total > 10000;
Validate login infoSELECT * FROM users WHERE email = 'admin@xyz.com' AND password = 'abc123';
Check for date rangesSELECT * FROM logs WHERE login_date BETWEEN '2024-01-01' AND '2024-12-31';

Summary โ€“ Recap & Next Steps

The WHERE clause enables precision in your MySQL queries. It narrows down results, making queries efficient and meaningful.

Key Takeaways

  • Use WHERE with SELECT, UPDATE, DELETE, and INSERT
  • Combine with logical operators (AND, OR, NOT)
  • Supports advanced filters: IN, BETWEEN, LIKE, IS NULL
  • Makes your queries targeted and efficient

Real-World Relevance

From filtering out inactive users to querying high-value orders, the WHERE clause is the core of decision-making in SQL.


FAQ โ€“ MySQL WHERE Clause

Can I use WHERE with multiple conditions?

Yes, combine them using AND, OR, and parentheses.

Whatโ€™s the difference between = and IN?

= checks for a single match, while IN checks multiple:

WHERE city = 'Delhi'          -- Single
WHERE city IN ('Delhi','Pune') -- Multiple

How do I use WHERE with dates?

Use the standard format 'YYYY-MM-DD':

WHERE order_date = '2025-05-01'

Can I use WHERE with NULL values?

Yes. Use IS NULL or IS NOT NULL:

WHERE email IS NOT NULL;

What happens if I forget the WHERE clause in UPDATE or DELETE?

It will affect all rows in the table! Always double-check.


Share Now :
Share

๐Ÿ“Œ MySQL WHERE Clause

Or Copy Link

CONTENTS
Scroll to Top