🔎 Filtering Data
The SQL WHERE clause is used to filter records that meet specific conditions. Instead of returning the entire table, WHERE
helps you narrow down results to only what you need.
This is one of the most powerful features in SQL because data filtering is at the heart of every real-world query.
1️⃣ Basic WHERE Example
Fetch employees who earn more than 60000
:
SELECT FirstName, LastName, Salary
FROM Employees
WHERE Salary > 60000;
2️⃣ Using Equality (=)
SELECT *
FROM Employees
WHERE Department = 'IT';
3️⃣ Multiple Conditions with AND / OR
SELECT *
FROM Employees
WHERE Department = 'Sales'
AND Salary > 50000;
SELECT *
FROM Employees
WHERE Department = 'HR'
OR Department = 'Finance';
4️⃣ NOT Operator
SELECT *
FROM Employees
WHERE NOT Department = 'Admin';
5️⃣ Comparison Operators
Operator | Description | Example |
---|---|---|
= | Equal | WHERE Salary = 60000 |
<> or != | Not Equal | WHERE Department <> ‘IT’ |
> | Greater Than | WHERE Salary > 60000 |
< | Less Than | WHERE Salary < 30000 |
>= | Greater or Equal | WHERE Salary >= 40000 |
<= | Less or Equal | WHERE Salary <= 45000 |
6️⃣ Combining WHERE with ORDER BY
SELECT FirstName, Salary
FROM Employees
WHERE Salary BETWEEN 50000 AND 80000
ORDER BY Salary DESC;
Pro Tip: Always use
WHERE
before ORDER BY
, otherwise your query will throw an error.✅ Key Takeaways
WHERE
filters rows based on conditions.- Supports operators like
=, !=, >, <, BETWEEN, LIKE
. - Use
AND
/OR
to combine multiple conditions. NOT
inverts a condition.- Always place
WHERE
beforeORDER BY
in SQL syntax.
📚 Continue Learning
💬 Got a question about SQL WHERE clause? Drop it in the comments below and let’s discuss!