T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL - WHERE Keyword



The T-SQL (Transact-SQL) WHERE keyword is used to specify condition(s) in 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 keyword in T-SQL (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:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • 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:

    NameAgeSalary
    John253000
    Kim303100
    Ramesh283000
  • 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 T-SQL AND operator is used and the query is:

    SELECT * FROM Employee
    WHERE Salary > 2800 AND Age < 30;
    

    This result of the following code will be:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    5RameshNew Delhi283000

❮ T-SQL Keywords