OR conditions in SQL Server
The OR condition in SQL Server (Transact-SQL) is used to 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ện
n'
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.
For example - SELECT 2 condition command
SELECT *
FROM nhanvien
WHRE ten = 'Sarah'
OR ho = 'J
ohnson';
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.
For example - SELECT 3 condition command
SELECT ho, ten
FROM nhanvien
WHERE ho = 'Anderson'
OR bang = 'California'
OR nhanv
ien_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, ten
FROM nhanvien
WHERE ho = 'Smith'
OR nhanvien_i
d <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
UPDATEen
nhanvi
SET bang = 'Florida'
WHERE nhanvien_id < 1000
OR thanhpho = 'Mia
mi';
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 nhanvien
WHERE 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
You should read it
May be interested
- Combine AND and OR conditions in SQL Serverthe article explains how to use and conditions and or conditions in sql server (transact-sql).
- DISTINCT clause in SQL Serverthe distinct clause is used to remove duplicates in the result set.
- Introduction to SQL Server Reporting Servicessql server 2005, 2008 and 2008 r2 software packages are pre-packaged with sql server reporting services (ssrs) - a specialized report creation solution for businesses. with ssrs, you can completely create, publish, and manage a huge number of reports from various data sources ...
- Instructions for creating and editing reports in SQL Server Reporting Servicessql server 2005, 2008 and 2008 r2 product suite, all packaged with sql server reporting services (ssrs) - a dedicated solution for creating reports for businesses. with ssrs, users can create, schedule, post and manage various reports from various sources of original data.
- SQL Server setup is always availabledatabase mirroring solution helps build a high-availability database management system in sql server which is quite simple and suitable for medium-sized and lower-level databases.
- Segment tables in SQL Servertable partitioning technique (table partitioning) to effectively manage the database with large capacity.