Combine AND and OR conditions in SQL Server

The article explains how to use AND conditions and OR conditions in SQL Server (Transact-SQL).

There are separate tutorials on AND conditions and OR conditions in SQL Server. But in addition, these two conditions can be used in combination with SELECT, INSERT, UPDATE and DELETE commands.

When combining these two conditions, it is important to remember to use parentheses to let the database know the order in which to execute each condition.

Syntax combining AND conditions and OR conditions

 WHERE 'điều kiện 1' 
AND 'điều kiện 2'

OR 'điều kiện n';

Variable names and variable values

Condition 1, Condition 2 . Condition n

Conditions are evaluated to determine if the record is selected.

Note

  1. AND and OR conditions allow multiple conditions to be checked
  2. Don't forget the order of execution, determined by parentheses

For example - SELECT command

 SELECT * 
FROM nhanvien
WHERE (ho = 'Anderson' AND ten = 'Sarah')
OR (nhanvien_id = 75);

This command will return all employees with the last name Anderson and the name Sarah or have ID 75. Parentheses determine the order in which the conditions are executed.

 SELECT nhanvien_i d, ho, ten 
FROM nhanvien
WHERE (ho = 'Smith')
OR (ho = 'Anderson' AND ten = 'Sarah')
OR (nhanvien_id > 1000 AND ba ng = 'California');

In this example, the result returns the employee ID, the last name and the first name if that person is Smith; or they are Anderson and his name is Sarah; or employee ID greater than 1000 and state is California.

Example - INSERT command

 INSERT INTO danhba 
(ho, ten)
SELECT ho, ten
FROM nhanvien
WHERE (ho = 'Johnson' OR ho = 'Anderson')
AND nhanvien_id > 54;

This example will insert into the list all the values ​​of the family name and name from those who have the last name Johnson or Anderson and whose ID is greater than 54

Example - UPDATE command

 UPDATE nhanvien 
SET ho = 'TBD'
WHERE nhanvien_id <= 2000
AND (bang = 'California' OR bang = 'Arizona');

In this order, the employee's surname will be updated to TBD if the employee ID is less than or equal to 2000 and lives in California or Arizona.

Example - DELETE command

 DELETE FROM nhanvien 
WHERE bang = 'California'
AND (ho = 'Johnson' OR ten = 'Joe');

In this example, the combination of the AND and OR conditions in the DELETE command will delete all records in the table if the state value is California and the employee has the last name Johnson or the name Joe.

Previous article: OR conditions in SQL Server

Next lesson: DISTINCT clause in SQL Server

4.5 ★ | 2 Vote

May be interested

  • The difference between web server and app serverThe difference between web server and app server
    you have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
  • 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.
  • Combine 2 networks to speed upCombine 2 networks to speed up
    today's article will show you how to combine two or more internet networks into one main network. in doing so, you will divide the download speed for two or more available internet connections so that your overall browsing speed is not affected by large file downloads or continuous streaming.
  • How to combine photos in Word, combine 2 photos into 1 frameHow to combine photos in Word, combine 2 photos into 1 frame
    when using word, besides being able to insert individual images into the document, you can also use some tips to combine images in word simply and easily without having to use editing tools. other images like photoshop, paint,... just follow tipsmake's instructions and you can do it yourself.
  • How to combine layers in PhotoshopHow to combine layers in Photoshop
    sometimes it is necessary to combine or merge layers together to work on merged images or flatten all layers and form a single layer. luckily, there are several options available for combining layers in photoshop.
  • 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 combine multiple screen shots into a single photo on iPhone and iPadHow to combine multiple screen shots into a single photo on iPhone and iPad
    tailor is a third-party application for iphone, which helps users to easily combine screenshots. follow the instructions below to combine multiple iphone screenshots into a single image.
  • 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.
  • How to combine Vlookup function with If function in ExcelHow to combine Vlookup function with If function in Excel
    when combining vlookup function with if function in excel, we can set conditions to search for values ​​by column.