Filtering helps you narrow down results based on specific conditions.
Let’s explore some powerful SQL operators:
1️⃣ IN – Match multiple values
Syntax:
SELECT columns FROM table_name WHERE column IN (value1, value2,...);
Example:
Get customers from specific cities:
SELECT name, city FROM customers
WHERE city IN ('Delhi', 'Mumbai', 'Chennai');
2️⃣ OR – Match any of multiple conditions
Syntax:
SELECT columns FROM table_name WHERE condition1 OR condition2;
Example:
Get employees from HR or Finance:
SELECT name FROM employees
WHERE department = 'HR' OR department = 'Finance';
3️⃣ AND – Match all conditions
Syntax:
SELECT columns FROM table_name WHERE condition1 AND condition2;
Example:
Get Sales employees earning more than 60,000:
SELECT name FROM employees
WHERE department = 'Sales' AND salary > 60000;
4️⃣ NOT – Exclude specific values or conditions
Syntax:
SELECT columns FROM table_name WHERE NOT condition;
Example:
Get all products except Electronics:
SELECT * FROM products
WHERE NOT category = 'Electronics';
5️⃣ BETWEEN – Match a range of values (inclusive)
Syntax:
SELECT columns FROM table_name WHERE column BETWEEN value1 AND value2;
Example:
Get employees with salary between 50,000 and 100,000:
SELECT name, salary FROM employees
WHERE salary BETWEEN 50000 AND 100000;
🔥 Quick Practice Task:
Write a query to:
• Get all employees in 'IT' or 'HR'
• Who earn more than 50,000
• Show name, department, and salary:
SELECT name, department, salary
FROM employees
WHERE department IN ('IT', 'HR')
AND salary > 50000;
✅ SQL Filtering Interview QA 💼🧠
Q1. What’s the difference between AND and OR?
A:
• AND requires all conditions to be true
• OR requires at least one condition to be true
Q2. Can you combine AND and OR in one query?
A: Yes, but use parentheses to control logic:
SELECT * FROM employees
WHERE (department = 'Sales' OR department = 'Marketing')
AND salary > 60000;
Q3. What does NOT IN do?
A: Excludes rows with values in the list:
SELECT * FROM customers
WHERE city NOT IN ('Delhi', 'Mumbai');
Q4. Can BETWEEN be used with dates?
A: Absolutely!
SELECT * FROM orders
WHERE order_date BETWEEN '2025-01-01' AND '2025-01-31';
Q5. What’s the difference between IN and multiple ORs?
A: IN is cleaner and more concise:
-- Instead of:
WHERE city = 'A' OR city = 'B' OR city = 'C';
-- Use:
WHERE city IN ('A', 'B', 'C');💡 Pro Tip:
When combining multiple filters, always use parentheses to avoid unexpected results due to operator precedence.
SQL Roadmap
DOUBLE TAP ❤️ FOR MORE