🔍 SQL WHERE Clause – Filter Data Like a Pro

Want to select only the rows that matter from your table?
The SQL WHERE clause lets you filter records based on specific conditions—essential for making your queries more precise and powerful.


🧾 What is the WHERE Clause in SQL?

The WHERE clause in SQL is used to filter rows in a table based on one or more conditions.
It works with SELECT, UPDATE, DELETE, and more.

📌 Only the rows that meet the condition(s) are included in the result.


✅ SQL WHERE Syntax

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

Replace condition with a logical expression that returns TRUE or FALSE.


📚 Example: Filter Employees by Department

SELECT first_name, last_name, department
FROM employees
WHERE department = 'Sales';

This query returns only those employees working in the Sales department.


🔢 Operators You Can Use with WHERE

OperatorDescriptionExample
=Equal tosalary = 50000
>, <, >=, <=Greater / Less Thanage > 30
<> or !=Not equal todepartment <> 'HR'
BETWEENWithin a rangesalary BETWEEN 30000 AND 50000
INMatch from a listdepartment IN ('Sales', 'IT')
LIKEMatch a patternname LIKE 'A%'
IS NULLCheck for NULL valuesmanager_id IS NULL
AND, OR, NOTCombine conditionsage > 30 AND department = 'IT'

🔁 Example: Filter with Multiple Conditions

SELECT first_name, last_name, salary
FROM employees
WHERE department = 'Marketing'
AND salary > 60000;

Returns Marketing employees with a salary greater than 60,000.


🧠 Example: Pattern Matching with LIKE

SELECT customer_name
FROM customers
WHERE customer_name LIKE 'J%';

🔍 Finds all customers whose names start with ‘J’.


⚠️ NULLs Are Special

SELECT *
FROM employees
WHERE manager_id IS NULL;

Use IS NULL instead of = NULL—SQL treats NULL as “unknown”, not equal to anything (even itself).


🧪 Challenge: Practice Time

Question: Retrieve all products that cost more than ₹1000 and are in stock.

SELECT product_name, price, stock
FROM products
WHERE price > 1000 AND stock > 0;

📝 Final Thoughts

The WHERE clause is the foundation of data filtering in SQL. It allows you to:

  • Retrieve only relevant rows
  • Filter based on multiple criteria
  • Power dynamic reporting and dashboards

✅ Combine with ORDER BY, LIMIT, or JOIN
✅ Use logical operators (AND, OR, NOT) for complex conditions