SQL WHERE Clause
The `WHERE` clause in SQL is used to filter records and fetch only those rows that satisfy a specified condition. It is used with SQL statements like `SELECT`, `UPDATE`, and `DELETE`.
Syntax
The basic syntax of the `WHERE` clause is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
Suppose you have a table named Employees
with a column named Age
. To get the records of employees who are 30 years old, you would use the following query:
SELECT *
FROM Employees
WHERE Age = 30;
Operators
The `WHERE` clause can use various operators to filter records. Some common operators include:
- = - Equal to
WHERE column = value
- <> - Not equal to
WHERE column <> value
- > - Greater than
WHERE column > value
- < - Less than
WHERE column < value
- BETWEEN - Within a range
WHERE column BETWEEN value1 AND value2
- LIKE - Pattern matching
WHERE column LIKE pattern
- IN - Match any value in a list
WHERE column IN (value1, value2, ...)
Notes
- Case Sensitivity: In SQL, string comparisons can be case-sensitive or case-insensitive, depending on the database system.
- Combining Conditions: You can combine multiple conditions using operators like AND, OR, and NOT to filter records more precisely.
Understanding how to use the `WHERE` clause effectively will allow you to filter data according to your needs, making your queries more precise and efficient.