π SQL WHERE β Filter Data Based on Conditions
π§² Introduction β Why the WHERE Clause is Essential
The WHERE clause in SQL lets you filter rows based on conditions, allowing you to extract only the relevant records from your data. Without WHERE, queries return all rows β which may be unnecessary, inefficient, or even risky.
Whether you’re validating user logins, querying sales by region, or excluding null values, WHERE is the go-to clause for data precision and control.
π― In this guide, youβll learn how to:
- Write basic
WHEREconditions - Use logical operators (
AND,OR,NOT) - Apply comparison and pattern-matching operators
- Filter null values
- Combine
WHEREwith other clauses
β 1. Basic WHERE Syntax
SELECT *
FROM table_name
WHERE condition;
β Filters rows based on the specified condition.
π 2. Comparison Operators in WHERE
= -- Equal to
!= or <> -- Not equal to
> -- Greater than
< -- Less than
>= -- Greater than or equal to
<= -- Less than or equal to
Example:
SELECT * FROM products WHERE price > 100;
π 3. Logical Operators: AND, OR, NOT
SELECT * FROM employees
WHERE department = 'Sales' AND salary > 50000;
β Combines multiple conditions.
π 4. Pattern Matching with LIKE
SELECT * FROM customers
WHERE name LIKE 'A%';
β
% matches zero or more characters. _ matches a single character.
π 5. IN, BETWEEN, and NOT Operators
-- IN
SELECT * FROM orders WHERE status IN ('Shipped', 'Delivered');
-- BETWEEN
SELECT * FROM employees WHERE salary BETWEEN 40000 AND 60000;
-- NOT
SELECT * FROM users WHERE NOT active = 1;
β Great for concise multi-value filters.
π§Ή 6. Filtering NULL Values
SELECT * FROM students
WHERE grade IS NULL;
β
Use IS NULL or IS NOT NULL to handle missing data.
π Best Practices
| β Do This | β Avoid This |
|---|---|
| Use precise filters | Omitting WHERE when modifying data |
| Combine logical operators carefully | Mixing AND/OR without parentheses |
| Handle NULLs explicitly | Assuming NULL = ” or NULL = 0 |
| Use indexed columns for WHERE | Filtering on computed expressions |
π Summary β Recap & Next Steps
The WHERE clause is fundamental for retrieving and managing targeted data in SQL. It ensures you only interact with rows that meet specific criteria.
π Key Takeaways:
WHEREfilters rows based on conditions- Supports logical, comparison, and pattern operators
- Use with
IN,BETWEEN,LIKE,IS NULL, etc. - Prevents unnecessary operations on large datasets
βοΈ Real-World Relevance:WHERE is used in authentication, report filters, dashboards, and backend logic across all modern database systems.
β‘οΈ Up next: Learn how to sort query results with ORDER BY.
β FAQ β SQL WHERE Clause
β Can I use multiple conditions in WHERE?
β
Yes. Use AND, OR, and NOT to combine them.
β Whatβs the difference between = and LIKE?
β
= is for exact matches. LIKE allows pattern-based searching.
β How do I check for empty or NULL values?
β
Use IS NULL or IS NOT NULL.
β Can WHERE be used with UPDATE and DELETE?
β Yes, and itβs essential. Without it, all rows will be updated or deleted.
β Is WHERE case-sensitive?
β It depends on the collation and database engine. MySQL is case-insensitive by default.
Share Now :
