Filtering Data Using WHERE Clause
Introduction
In real-world data analysis, you rarely need all the data. You often need specific records based on conditions. The WHERE clause in SQL helps you filter data and retrieve only relevant information.
What is WHERE Clause
The WHERE clause is used with the SELECT statement to filter records based on specific conditions. It allows you to extract only the data that meets certain criteria.
Basic syntax:
SELECT column_name FROM table_name WHERE condition;
Filtering with Conditions
You can filter data using conditions such as equality, comparison, and logical operators.
Example:
SELECT * FROM employees WHERE salary > 30000;
This query returns employees with salary greater than 30000.
Common Operators Used in WHERE
- = (equal to)
-
(greater than)
- < (less than)
-
= (greater than or equal to)
- <= (less than or equal to)
- != (not equal to)
Example:
SELECT name FROM employees WHERE department = ‘IT’;
Using AND, OR, NOT
You can combine multiple conditions using logical operators.
Example using AND:
SELECT * FROM employees WHERE department = ‘IT’ AND salary > 40000;
Example using OR:
SELECT * FROM employees WHERE department = ‘HR’ OR department = ‘IT’;
Example using NOT:
SELECT * FROM employees WHERE NOT department = ‘Sales’;
Filtering Text Data
When filtering text values, always use single quotes.
Example:
SELECT * FROM customers WHERE city = ‘Jaipur’;
Why WHERE Clause is Important
The WHERE clause helps you:
- Extract specific data from large datasets
- Perform accurate data analysis
- Reduce unnecessary data processing
- Build meaningful reports
Best Practices
- Use clear and precise conditions
- Avoid unnecessary filters
- Combine conditions carefully
- Test queries on small datasets
Summary
In this lesson, you learned how to filter data using the WHERE clause. This is a key concept in SQL for data analysis and helps you retrieve meaningful insights from large datasets. In the next lesson, you will learn how to sort data using ORDER BY.
FAQs
1. What is WHERE clause in SQL?
It is used to filter records based on specific conditions.
2. Can we use multiple conditions in WHERE?
Yes, using AND, OR, and NOT operators.
3. Is WHERE case-sensitive?
It depends on the database configuration, but generally text comparisons may be case-sensitive.
4. Can WHERE be used without SELECT?
No, it is usually used with SELECT, UPDATE, or DELETE statements.
Internal Link
Want to explore more courses?
Click here for more free courses



