πŸ“„ SQL Basics – Core Queries & Clauses
Estimated reading: 3 minutes 49 views

πŸ“Œ 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 WHERE conditions
  • Use logical operators (AND, OR, NOT)
  • Apply comparison and pattern-matching operators
  • Filter null values
  • Combine WHERE with 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 filtersOmitting WHERE when modifying data
Combine logical operators carefullyMixing AND/OR without parentheses
Handle NULLs explicitlyAssuming NULL = ” or NULL = 0
Use indexed columns for WHEREFiltering 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:

  • WHERE filters 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ“Œ SQL WHERE

Or Copy Link

CONTENTS
Scroll to Top