WHERE clause in SQL Server

In SQL Server) T-SQL), the WHERE clause is used to filter results from SELECT, INSERT, UPDATE, or DELETE statements.

In SQL Server (T-SQL), the WHERE clause is used to filter results from SELECT, INSERT, UPDATE, or DELETE statements.

WHERE clause syntax

 WHERE 'điều kiện'; 

Variable names or variable values

Condition - The conditions that the record must meet to be selected.

For example - a condition

 SELECT * 
FROM nhanvien
WHERE ten = 'Jane';

In the WHERE clause above, we filter out the results from the table. The SELECT statement returns rows from the table named Jane. Since using * in the SELECT statement, all fields in the table will appear in the result set.

For example - using AND conditions

 SELE CT * 
FROM nhanvien
WHERE ho = 'Anderson'
AND nhanvien_id >= 3 000;

The above clause uses two conditions with the AND keyword. The SELECT statement above will return the employees whose last name is Anderson and have a number greater than or equal to 3000.

For example - use an OR condition

 SELECT nhanvien_id, h o, ten 
FROM nhanvien
WHERE ho = 'Johnson'
OR ten = 'Danielle';

In this example, the SELECT statement returns the following values: the ID of the employee, the first and last name from the last table with the last name Johnson or the name Danielle.

Example - combine conditions AND and OR

 SELE CT * 
From nhanvien
WHERE (bang = 'California' AND ho = 'Smith')
OR (nhanv ien_id = 82);

The above example results in employees living in California and having the surname Smith or having the same name as 82. Parentheses are used to determine the order in which each condition is executed (as in math).

Example - table combination

 SELECT nhanvien.nhanvien_id, danhba.ho 
FROM nhanvien
INNER JOIN danhba
ON nhanvien.nhanvien_id = danhba.danhba_id
WHERE nhanvien.tan = 'Sarah';

The SELECT statement above will return the rows named in the table as Sarah. The table of names and names is connected by the user_id in the table and the list in the list.

Previous article: Comparison operators in SQL Server

Next lesson: SQL ORDER BY clause

4 ★ | 1 Vote