WHERE
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.
WHERE
clause is used to filter rows based on one or more conditions.=
, >
, <
), logical operators (e.g., AND
, OR
, NOT
), and pattern matching (e.g., LIKE
).WHERE
clause can improve query performance by reducing the number of rows processed.WHERE
clause can be used with SELECT
, UPDATE
, DELETE
, and other SQL statements.column1, column2, ...
: The columns you want to retrieve.table_name
: The table from which to retrieve data.condition
: The criteria used to filter rows.Employees
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 |
AND
, 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 |
=
, >
, <
, >=
, <=
, 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 |
LIKE
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 |
IN
operator allows you to specify multiple values in a WHERE
clause.
To retrieve employees from Chennai or Madurai:
Name | City |
---|---|
Anand | Chennai |
Raj | Madurai |
WHERE
clause is used to filter rows based on specified conditions.=
, >
, <
, etc.), logical operators (AND
, OR
, NOT
), and pattern matching (LIKE
).IN
operator allows filtering based on multiple values.WHERE
clause improves query performance by reducing the number of rows processed.WHERE
clause is essential for extracting specific data from tables.