WHERE Clause | Dataplexa

WHERE Clause

In real-world databases, we rarely want to see all the data. The WHERE clause allows us to filter rows based on specific conditions.

This is one of the most frequently used features in SQL.


What is the WHERE Clause?

The WHERE clause is used to specify conditions that rows must meet in order to be included in the result set.

Basic structure:

SELECT column_name
FROM table_name
WHERE condition;
  

The database checks each row and returns only those that satisfy the condition.


Example Table

We will use the employees table again:

+----+----------+------------+--------+
| id | name     | department | salary |
+----+----------+------------+--------+
| 1  | Alice    | HR         | 60000  |
| 2  | Bob      | IT         | 75000  |
| 3  | Charlie  | Finance    | 70000  |
| 4  | Diana    | IT         | 80000  |
+----+----------+------------+--------+
  

Filtering with WHERE

To retrieve only employees from the IT department:

SELECT * FROM employees
WHERE department = 'IT';
  

This query returns only the rows where the department column equals 'IT'.


Comparison Operators

WHERE clauses commonly use comparison operators.

Operator Meaning Example
= Equal to salary = 70000
!= or <> Not equal to department != 'HR'
> Greater than salary > 60000
< Less than salary < 80000
>= Greater than or equal salary >= 70000
<= Less than or equal salary <= 75000

Filtering Numeric Values

To get employees earning more than 70,000:

SELECT name, salary
FROM employees
WHERE salary > 70000;
  

Only rows with salary values greater than 70,000 will be returned.


Filtering Text Values

When filtering text (strings), values must be enclosed in single quotes.

SELECT name, department
FROM employees
WHERE department = 'Finance';
  

Text comparisons are usually case-insensitive in MySQL.


Common Beginner Mistakes

  • Forgetting single quotes around text values
  • Using = instead of LIKE for patterns
  • Misspelling column or table names
  • Using WHERE before FROM

Remember: SQL keywords are flexible, but structure matters.


Why WHERE Clause is Important

The WHERE clause allows you to:

  • Analyze specific data subsets
  • Improve query performance
  • Retrieve meaningful results
  • Build advanced logic with conditions

Almost every real SQL query includes a WHERE clause.


What’s Next?

In the next lesson, we will combine multiple conditions using AND, OR, and NOT to create more powerful filters.