๐ SQL Fundamentals Part-2: Filtering
After learning SELECT basics, the next step is learning how to filter data.
๐ In real-world data analysis, you rarely need full data โ you filter specific rows.
Filtering = extracting only relevant data from a table.
โ
What is Filtering in SQL?
Filtering is done using the WHERE clause.
It allows you to:
โ Get specific records
โ Apply conditions
โ Clean data
โ Extract business insights
๐น 1. Comparison Operators
Used to compare values.
Operator Meaning
โข = Equal
โข > Greater than
โข < Less than
โข >= Greater than or equal
โข <= Less than or equal
โข != or <> Not equal
โ
Examples
โข Equal to
SELECT * FROM employees WHERE city = 'Pune';
โข Greater than
SELECT * FROM employees WHERE salary > 50000;
โข Not equal
SELECT * FROM employees WHERE department != 'HR';
๐ก Most commonly used in dashboards reporting.
๐น 2. Logical Operators (AND, OR, NOT)
Used to combine multiple conditions.
โ
AND โ Both conditions must be true
SELECT * FROM employees WHERE salary > 50000 AND city = 'Mumbai';
๐ Returns employees with: salary > 50000 AND located in Mumbai
โ
OR โ Any condition can be true
SELECT * FROM employees WHERE city = 'Delhi' OR city = 'Pune';
๐ Returns employees from either city.
โ
NOT โ Reverse condition
SELECT * FROM employees WHERE NOT department = 'Sales';
๐ Excludes Sales department.
๐น 3. BETWEEN (Range Filtering)
Used to filter values within a range.
Syntax
SELECT * FROM table WHERE column BETWEEN value1 AND value2;
โ
Example
SELECT * FROM employees WHERE salary BETWEEN 30000 AND 70000;
๐ Includes boundary values.
๐น 4. IN Operator (Multiple Values Shortcut)
Better alternative to multiple OR conditions.
โ Without IN
WHERE city = 'Pune' OR city = 'Delhi' OR city = 'Mumbai'
โ
With IN
SELECT * FROM employees WHERE city IN ('Pune','Delhi','Mumbai');
๐ Cleaner and faster.
๐น 5. LIKE โ Pattern Matching
Used for searching text patterns.
โญ Wildcards
Symbol Meaning
โข % Any number of characters
โข _ Single character
โ
Starts with "A"
SELECT * FROM customers WHERE name LIKE 'A%';
โ
Ends with "n"
WHERE name LIKE '%n';
โ
Contains "an"
WHERE name LIKE '%an%';
Used heavily in search features.
๐น 6. NULL Handling (Very Important โญ)
NULL means:
๐ Missing / unknown value
๐ Not zero
๐ Not empty
โ Wrong
WHERE salary = NULL
โ
Correct
SELECT * FROM employees WHERE salary IS NULL;
Check non-null values
WHERE salary IS NOT NULL;
๐ก Very common interview question.
โญ Order of Filtering Execution
SQL processes filtering after reading table:
FROM โ WHERE โ SELECT โ ORDER BY โ LIMIT
๐ง Real-World Data Analyst Examples
Q. Find customers from Pune
SELECT * FROM customers WHERE city = 'Pune';
Q. Find high-paying jobs in IT department
SELECT * FROM employees WHERE salary > 80000 AND department = 'IT';
Q. Find names starting with "R"
SELECT * FROM employees WHERE name LIKE 'R%';
Used daily in business analytics.
๐ Mini Practice Tasks
โ
Q1
Find employees whose salary is greater than 60000.
โ
Q2
Find customers from Pune or Mumbai.
โ
Q3
Find products priced between 100 and 500.
โ
Q4
Find employees whose name starts with "S".
โ
Q5
Find records where email is missing (NULL).
โ
Double Tap โฅ๏ธ For More
Post #2345
5.18K
- โค 8