T-SQL WHERE Clause

Where clause is used to generate the conditions while obtaining data from a table or including it in other tables. If the condition is satisfied, then it returns a particular value from the table. We use WHERE clause to filter the records in database and to fetch the main records.

In a SELECT statement, we use the WHERE clause, but it also used in UPDATE, DELETE account, etc.

Syntax:

We are generating a condition using the logical operators, which are: >, <, =, LIKE, NOT etc.

Lets understand it using below example:

Example:

See the EMPLOYEES table which has the following records -

IDNameAGEADDRESSSalary
001Rahul23Kota20000.00
002Clinton22Mumbai15000.00
003Kamal31Delhi25000.00
004Chitra28Kanyakumari65000.00
005Santanu26Madhya Pradesh38500.00
006Savitri24Bhopal4500.00
007Manii30Indonesia15000.00

The following command is an example that would fetch the ID, Name, and Salary fields from the EMPLOYEES table where salary is more significant than 2000.

Output of above query:

IDNAMESALARY
001Rahul22000.00
003Kamal25000.00
004Chitra65000.00
005Santanu38500.00

EXAMPLE: 1

The command fetches NAME, AGE, and Salary fields from the EMPLOYEES table. Where the name of employee is 'Chitra.'

All strings must be generated inside the single quotes ('') where the numeric values used without any quote:

The command generates the given output.

NAMEAGESALARY
Chitra2865000.00

EXAMPLE: 2

The command fetches ID, and AGE fields from the EMPLOYEES table. Where the name of employee is 'Chitra.'

The command generates the given output.

IDAGE
00730

EXAMPLE 3:

The below command is an example, which fetch the ID, Name, and Salary fields from the EMPLOYEES table where AGE is greater than 28.

Output:

IDNAMESALARY
003Kamal25000.00
007Manii15000.00