SQL WHERE Clause 🔍¶
Mentor's Note: The
WHEREclause is the "Security Guard" of your query. It checks every row and asks, "Do you meet my criteria?" If yes, the row passes through. If no, it's blocked from the result set. 💡
🌟 The Scenario: The VIP List 🎫¶
Imagine you are the host of a party. - No WHERE Clause: "Everyone in the city is invited!" (Your house will crash! 🤯) - WHERE Age >= 18: "Only adults are invited." ✅ - WHERE City = 'Surat': "Only locals are invited." ✅
🎨 Visual Logic: The Sieve¶
graph TD
A[(Table: 1000 Rows)] -- "WHERE salary > 50000" --> B[Result Set: 50 Rows]
A -- "Row 1: 30k" --> C{Pass?}
C -- "No" --> D[Discard 🗑️]
A -- "Row 2: 60k" --> E{Pass?}
E -- "Yes" --> F[Include in Result 📊]
💻 1. The Basic Syntax¶
Example: Filtering by Text¶
-- Scenario: Find all IT department employees
SELECT emp_name, salary
FROM employees
WHERE department = 'IT';
Example: Filtering by Numbers¶
-- Scenario: Find products with low stock
SELECT product_name, stock_count
FROM warehouse
WHERE stock_count < 10;
💻 2. Combining Conditions (AND/OR) 🔗¶
You can use multiple filters at once.
-- Scenario: Find High-Earning IT staff
SELECT * FROM employees
WHERE department = 'IT'
AND salary > 80000;
💻 3. Filtering by Date 📅¶
Dates must be in single quotes.
-- Scenario: Find students who joined recently
SELECT name FROM students
WHERE join_date > '2026-01-01';
💡 Pro Tip: Performance¶
"The
WHEREclause is your best friend for performance. By filtering data at the source, you reduce the amount of data the computer has to process and send over the network!"