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

  1. The OR condition in SQL Server allows two or more conditions to be checked.
  2. 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

 SELE CT * 
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

 SEL ECT 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 d anhba 
(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

 UPDATE en 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

4 ★ | 1 Vote

May be interested

  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • How to set up your own Git server on LinuxHow to set up your own Git server on Linux
    while you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
  • Use IIS to set up FTP Server on WindowsUse IIS to set up FTP Server on Windows
    set up an ftp server (file transfer protocol server) to share and convert large files with unlimited traffic.
  • How to change DNS server on the most popular routersHow to change DNS server on the most popular routers
    changing the dns server settings on your router is not difficult, but every manufacturer uses their own custom interface, which means the process can be very different depending on which router you are owned.
  • What is VPS? VPS used to do? What is VPS different from Server?What is VPS? VPS used to do? What is VPS different from Server?
    what is vps? vps used to do? what is vps different from server ?. when you intend to learn about network data or open the website, you will definitely be introduced to many different server and server services. but server hosting has a lot of tricks
  • 7 great ideas using Raspberry Pi as a server7 great ideas using Raspberry Pi as a server
    raspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
  • Instructions for setting up and managing FTP Server on Windows 10Instructions for setting up and managing FTP Server on Windows 10
    if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.
  • Create VPN Server on Windows 8Create VPN Server on Windows 8
    no need to install any additional applications, you can easily 'turn' your computer into a vpn server if you're using windows 8. in this way, you can share data from the computer. as a simple lan system in the form of remote access. & a
  • What is the future of server virtualization?What is the future of server virtualization?
    server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.