What is the WHERE Clause?
TheWHERE clause in SQL is used to filter records based on specified conditions. It allows you to retrieve only those rows that meet the criteria defined in the WHERE clause. This is essential for extracting specific data from a table or multiple tables.
Key Points
- Filtering Data: The
WHEREclause is used to filter rows based on one or more conditions. - Condition Types: Conditions can include comparisons (e.g.,
=,>,<), logical operators (e.g.,AND,OR,NOT), and pattern matching (e.g.,LIKE). - Performance: Proper use of the
WHEREclause can improve query performance by reducing the number of rows processed. - The
WHEREclause can be used withSELECT,UPDATE,DELETE, and other SQL statements.
Syntax
column1, column2, ...: The columns you want to retrieve.table_name: The table from which to retrieve data.condition: The criteria used to filter rows.
Examples
Suppose you have a table namedEmployees that stores employee details.
Table: Employees
| EmployeeID | Name | City | State | Salary |
|---|---|---|---|---|
| 1 | Anand | Chennai | Tamil Nadu | 50000 |
| 2 | Bala | Coimbatore | Tamil Nadu | 60000 |
| 3 | Kavitha | Karaikal | Puducherry | 55000 |
| 4 | Raj | Madurai | Tamil Nadu | 70000 |
| 5 | Kumar | Trichy | Tamil Nadu | 45000 |
| Name | City | Salary |
|---|---|---|
| Bala | Coimbatore | 60000 |
| Kavitha | Karaikal | 55000 |
| Raj | Madurai | 70000 |
Students that stores student details, and you want to retrieve students from Tamil Nadu who scored more than 90%.
Table: Students
| StudentID | Name | City | State | Percentage |
|---|---|---|---|---|
| 1 | Ram | Chennai | Tamil Nadu | 92 |
| 2 | Karthik | Coimbatore | Tamil Nadu | 88 |
| 3 | David | Bangalore | Karnataka | 95 |
| 4 | Kannan | Karaikal | Puducherry | 91 |
| 5 | Siva | Madurai | Tamil Nadu | 89 |
| Name | City | Percentage |
|---|---|---|
| Ram | Chennai | 92 |
Using Logical Operators in the WHERE Clause
You can combine multiple conditions using logical operators likeAND, OR, and NOT.
To retrieve employees from Tamil Nadu who earn more than ₹50,000:
| Name | City | Salary |
|---|---|---|
| Bala | Coimbatore | 60000 |
| Raj | Madurai | 70000 |
| Name | City | Salary |
|---|---|---|
| Anand | Chennai | 50000 |
| Raj | Madurai | 70000 |
Using Comparison Operators
Comparison operators like=, >, <, >=, <=, and != are commonly used in the WHERE clause.
To retrieve employees who earn between ₹50,000 and ₹60,000:
| Name | City | Salary |
|---|---|---|
| Anand | Chennai | 50000 |
| Bala | Coimbatore | 60000 |
| Kavitha | Karaikal | 55000 |
Using the LIKE Operator for Pattern Matching
TheLIKE operator is used to filter rows based on patterns. It is often used with wildcard characters:
%: Matches zero or more characters._: Matches exactly one character.
| Name | City |
|---|---|
| Kavitha | Karaikal |
| Kumar | Trichy |
Using the IN Operator
TheIN operator allows you to specify multiple values in a WHERE clause.
To retrieve employees from Chennai or Madurai:
| Name | City |
|---|---|
| Anand | Chennai |
| Raj | Madurai |
Key Takeaways
- The
WHEREclause is used to filter rows based on specified conditions. - It supports comparison operators (
=,>,<, etc.), logical operators (AND,OR,NOT), and pattern matching (LIKE). - The
INoperator allows filtering based on multiple values. - Proper use of the
WHEREclause improves query performance by reducing the number of rows processed. - The
WHEREclause is essential for extracting specific data from tables.