Table of Contents
MS SQL Server: WHERE Clause in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
In SQL Server (T-SQL), the WHERE clause helps 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.
As an example, - a condition
SELECT*FROM nhanvienWHERE 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.
As an example, - using AND conditions
SELECT *FROM nhanvienWHERE ho = 'Anderson'AND nhanvien_id >= 3000;
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.
As an example, - use an OR condition
SELECT nhanvien_id, ho, tenFROM nhanvienWHERE 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
SELECT *From nhanvienWHERE (bang = 'California' AND ho = 'Smith')OR (nhanvien_id = 82);
The above example results in employees living in California and having the surname Smith or having the same name as 82. Parentheses help determine the order in which each condition is executed (as in math).
Example - table combination
SELECT nhanvien.nhanvien_id,danhba.hoFROM nhanvienINNER JOIN danhbaON nhanvien.nhanvien_id = danhba.danhba_idWHERE nhanvien.tan = 'Sarah';
Next lesson: SQL ORDER BY clause
FAQ
WHERE clause syntax WHERE '?i?u ki?n'; Variable names or variable values?
Condition - The conditions that the record must meet to be selected.
What is MS SQL Server: WHERE Clause in SQL Server?
In SQL Server (T-SQL), the WHERE clause helps filter results from SELECT, INSERT, UPDATE, or DELETE statements.
Why is MS SQL Server: WHERE Clause in SQL Server important?
A clear understanding of MS SQL Server: WHERE Clause in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Reader Comments 0
Sign in with email or Google to join the discussion.