Table of Contents
MS SQL Server: OR Conditions 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.
The OR condition in SQL Server (Transact-SQL) helps check multiple conditions to see if any record in the returned result meets the condition. It is often used in SELECT, INSERT, UPDATE or DELETE commands.
Syntax OR condition
WHERE '?i?u ki?n 1'OR '?i?u ki?n 2'…OR '?i?u ki?nn'
Variable name or variable value
Condition 1, Condition 2. Condition n
One of these conditions must be met, the new record is selected
Note
- The OR condition in SQL Server allows two or more conditions to be checked.
- The OR condition in SQL Server needs to meet any of those conditions (from 1 to n), the new record is returned in the result set.
As an example, - SELECT 2 condition command
SELECT *FROM nhanvienWHRE ten = 'Sarah'OR ho = 'Johnson';
The result will be all employees named Sarah or they are Johnson. Because of using * in the SELECT statement, all fields in the table will be returned in the result set.
As an example, - SELECT 3 condition command
SELECT ho, tenFROM nhanvienWHERE ho = 'Anderson'OR bang = 'California'OR nhanvien_id = 50;
In this example, the returned result is all the first and last names from the table, whose last name is Anderson or the state is California or the employee ID is 50.
Example - INSERT command
INSERT INTO danhba(danhba_id, ho, ten)SELECT nhanvien_id, ho, tenFROM nhanvienWHERE ho = 'Smith'OR nhanvien_id <10;
This command inserts all the list of employee IDs, last names and names from the employee table if they are Smith or an employee ID less than 10.
Example - UPDATE command
UPDATEennhanviSET bang = 'Florida'WHERE nhanvien_id < 1000OR thanhpho = 'Miami';
In the example above, the OR condition will update the state value in the table to Florida if it is less than 1000 or has the city of Miami.
Example - DELETE command
DELETE FROM nhanvienWHERE ten = 'Joanne'OR ten = 'Darlene';
The OR condition in this example will delete all employees in the table if the person's name is Joanne or Darlene.
Previous lesson: AND conditions in SQL Server
Next lesson: Combine AND and OR conditions in SQL Server
FAQ
What should you know about syntax OR condition WHERE '?i?u ki?n 1' OR '?i?u ki?n 2' … OR '?i?u ki?n n' Variable name or variable value?
Condition 1, Condition 2. Condition n.
What is MS SQL Server: OR Conditions in SQL Server?
The OR condition in SQL Server (Transact-SQL) helps check multiple conditions to see if any record in the returned result meets the condition.
Why is MS SQL Server: OR Conditions in SQL Server important?
A clear understanding of MS SQL Server: OR Conditions 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.