Skip to content

SQL Filtering (WHERE) πŸš€

Mentor's Note: A database can have millions of rows. WHERE is your "Magnifying Glass"β€”it helps you ignore the noise and find exactly the row you are looking for. πŸ’‘


🌟 The Scenario: The Police Investigation πŸ”

Imagine you are a detective searching for a specific car in a massive parking lot.

  • The Problem: There are 10,000 cars. You can't check them all. πŸ“¦
  • The Filter: You only want to see cars WHERE the color is 'Red' πŸ”΄ and the brand is 'Toyota' πŸš—.
  • The Result: Instead of 10,000 cars, you now have only 5 to check. This is exactly what the WHERE clause does in SQL. βœ…

πŸ“– Concept Explanation

1. The WHERE Clause

The WHERE clause is used to extract only those records that fulfill a specified condition.

2. Common Comparison Operators

Operator Meaning
= Equal to
<> or != Not equal
> Greater than
LIKE Search for a pattern (e.g. V%) πŸ”Ž
BETWEEN Within a range
IN Match any value in a list

🎨 Visual Logic: The Filter Funnel

graph TD
    A[10,000 Rows πŸ“‹] --> B{WHERE salary > 50000}
    B -- No --> C[Discarded πŸ—‘οΈ]
    B -- Yes --> D[50 Rows πŸ”]
    D --> E[Final Result βœ…]

πŸ’» Implementations (Dialect Comparison)

-- πŸš€ Action: Find employees in IT with high pay
SELECT name, salary
FROM employees
WHERE salary > 50000 
  AND department = 'IT';
-- πŸš€ Action: Same as Oracle
SELECT name, salary
FROM employees
WHERE salary > 50000 
  AND department = 'IT';
-- πŸš€ Action: Same as Oracle
SELECT name, salary
FROM employees
WHERE salary > 50000 
  AND department = 'IT';

πŸ“Š Sample Dry Run

Goal: Find students with marks > 80

Name Marks Condition (> 80) Action
Vishnu 95 βœ… True Keep
Ankit 75 ❌ False Discard

πŸ“ˆ Technical Analysis

  • Text Comparison: In Oracle, string comparison is case-sensitive ('IT' != 'it'). In MySQL, it depends on the database collation (often case-insensitive).
  • WILDCARDS:
    • % = Zero or more characters.
    • _ = Exactly one character.

🎯 Practice Lab πŸ§ͺ

Task: The Pattern Search

Task: Find all names in the USERS table that end with the letter 'a'. Hint: Use LIKE '%a'. πŸ’‘


πŸ’‘ Interview Tip πŸ‘”

"Interviewers love asking: 'How do you check for empty values?' Answer: Use IS NULL or IS NOT NULL. NEVER use = NULL because NULL is not a value, it is an absence of data!"


πŸ’‘ Pro Tip: "A program is like a poem: you build it from small parts, and it becomes a masterpiece." - Anonymous


← Back: Querying | Next: SQL Joins β†’