SQL Filtering Fundamentals: WHERE Clause and Beyond

🔍 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.

To view or add a comment, sign in

Explore content categories