The WHERE
clause in SQL Server is used to filter rows in a SELECT
, UPDATE
, or DELETE
statement.
It specifies which rows to include or exclude based on one or more conditions.
WHERE syntax
The syntax for the WHERE
clause is:
SELECT column_name(s)
FROM table_name
WHERE condition;
Where "condition" is an expression that evaluates to either true or false for each row in the table.
WHERE examples
For example, the following query retrieves all rows from the "employees" table where the "salary" is greater than 50000:
SELECT *
FROM employees
WHERE salary > 50000;
You can also use logical operators such as AND
and OR
to combine multiple conditions.
For example, the following query retrieves all rows from the "employees" table where the "salary" is greater than 50000 and the "department" is "IT":
SELECT *
FROM employees
WHERE salary > 50000 AND department = 'IT';
It's also possible to use subqueries in the WHERE
clause to filter the results based on the results of another query.
You can also use different comparison operators such as "=", ">", "<", ">=", "<=", "<>", "!=", "LIKE", "IN", "BETWEEN" and "EXISTS" in the WHERE clause.