SQL Server - WHERE Clause
The SQL Server (Transact-SQL) WHERE Clause is used to specify condition(s) in a query. It can be used to specify condition(s) while fetching data from a table, joining two tables, updating records in a table, inserting records in a table or deleting records from a table.
Syntax
The syntax for using WHERE Clause in SQL Server (Transact-SQL) is given below:
SELECT column1, column2, ... FROM table_name WHERE condition(s);
To specify condition in a query, comparison or logical operators like <, >, =, LIKE, IN, NOT, NULL etc. are used.
Example:
Consider a database containing a table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
-
To fetch Name, Age and Salary data from Employee table where Salary is greater than 2800, the query is:
SELECT Name, Age, Salary FROM Employee WHERE Salary > 2800;
This will produce the result as shown below:
Name Age Salary John 25 3000 Kim 30 3100 Ramesh 28 3000 -
To specify multiple conditions logical operators are used, for example - To fetch data from the Employee table where Salary is greater than 2800 and Age is less than 30, the SQL Server AND operator is used and the query will be:
SELECT * FROM Employee WHERE Salary > 2800 AND Age < 30;
This result of the following code will be:
EmpID Name City Age Salary 1 John London 25 3000 5 Ramesh New Delhi 28 3000