IN conditions in SQL Server
The IN condition is used in SQL Server (Transact-SQL) to minimize the need to use too many OR conditions in SELECT, INSERT, UPDATE, or DELETE statements.
Syntax of IN conditions
biểu thức IN (giá trị 1, giá trị 2, … giá trị n);
Variable name or variable value
expression
Value to check
value 1, value 2, . value n
Values to check with expressions
Note
- The IN condition in SQL Server will return records when the expression has a value of 1, value 2, . or n value.
- The IN condition in SQL Server is also called the IN operator.
For example - with string value
SELECT*
FROM nhanvien
WHERE ho I
N ('Smith', 'Anderson', 'Johnson');
The result will be rows from the table if the employee's surname is Smith, Anderson or Johnson. Due to using * in the SELECT statement, all fields in the table of contents will be in the result set.
The above example is similar to the SELECT command below.
SELECT *
FROM nhanvien
WHERE ho = 'Smith'
OR ho = 'Anderson'
OR ho = 'Joh
nson';
Using the IN condition helps the command look shorter and easier to understand.
For example - with numerical values
SELECT *
FROM nhanvien
WHERE nha
nvien_id IN (1, 2, 3, 4, 10);
The returned result is the employee whose ID is 1, 2, 3, 4 or 10. The above command is equivalent to the following command.
SELECT*
FROM nhanvien
WHERE nhanvien_id = 1
OR nhanvien_id = 2
OR nhanvien_id = 3
OR nhanvien_id = 4
OR nhanvien_i
d = 10;
For example - use the NOT operator
SELECT *
FROM nhanvien
WHERE t
en NOT IN ('Sarah', 'John', 'Dale');
In the above example, the result set consists of rows from the table that have the employee name other than Sarah, John or Dale. Sometimes it is easier to find values that are not values you want. The above example is equivalent to the command below.
SELECT *
FROM nhanvien
WHERE ten <> 'Sarah'
AND ten <> 'John'
AND ten <> 'Dale';
Previous article: DISTINCT clause in SQL Server
Next lesson: IS NULL condition in SQL Server
You should read it
May be interested
- Network basics: Part 3 - DNS Servera 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 Linuxwhile 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 Windowsset 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 routerschanging 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 ?. 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 serverraspberry 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 2017the 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 10if 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 8no 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?server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.