Home » T-SQL WHERE Clause

T-SQL WHERE Clause

by Online Tutorials Library

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 –

ID Name AGE ADDRESS Salary
001 Rahul 23 Kota 20000.00
002 Clinton 22 Mumbai 15000.00
003 Kamal 31 Delhi 25000.00
004 Chitra 28 Kanyakumari 65000.00
005 Santanu 26 Madhya Pradesh 38500.00
006 Savitri 24 Bhopal 4500.00
007 Manii 30 Indonesia 15000.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:

ID NAME SALARY
001 Rahul 22000.00
003 Kamal 25000.00
004 Chitra 65000.00
005 Santanu 38500.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.

NAME AGE SALARY
Chitra 28 65000.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.

ID AGE
007 30

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:

ID NAME SALARY
003 Kamal 25000.00
007 Manii 15000.00

You may also like