INTERSECT operator in SQL Server
In SQL Server (Transact-SQL), INTERSECT operator is used to return records in both data sets or SELECT statements. If a record is available only in one query and not in the other, it will be removed from the result set of INTERSECT.
INTERSECT query
Illustrate the result returned from INTERSECT query
Explanation: INTERSECT query will return records located in the blue fill area. These records are in both database1 and database2.
Each SELECT in INTERSECT must have the same number of columns in the result set with the same data type.
INTERSECT operator syntax
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE dieu_kien]
INTERSECT
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE dieu_kie
n];
Variable name or variable value
bieu_thuc
The column or value you want to compare between the SELECT statements. They do not need to be in the same information field at each SELECT statement but the corresponding columns must have the same data.
state
Table wants to get records from there. Must have at least 1 table in the FROM clause.
WHERE dieu_kien
Option. Conditions must satisfy for the selected record.
Note:
- Two SELECT statements must have the same number of expressions.
- The corresponding column in each SELECT statement must have the same data type.
- The INTERSECT operator returns only the general record between SELECT statements.
For example - with 1 expression
SELECT sanpham_id
FROM sanpham
INTERSECT
SELECT sanpham_id
FROM hangtonk
ho;
In this example, if sanpham_id appears on both the sanpham and hangtonkho tables, it will be in the result set of INTERSECT.
Now add the WHERE condition to this query.
SELECT sanpham_id
FROM sanpham
WHERE sanpham_id >= 50
INTERSECT
SELECT sanpham_id
FROM hangtonkho
WHERE soluong >
0;
The first dataset will filter and return records in the dashboard and sanpham_id
greater than or equal to 50. The second data set will filter from the hangtonkho
table if the number is greater than 0.
For example - with multiple expressions
SELECT danhba_id, ho, ten
FROM danhba
WHERE ho = 'Anderson'
INTERSECT
SELECT nhanvien_id, ho, ten
FROM nh
anvien;
In this example, the query returns the result of the intersection of the two SELECT statements. If there is a record in the list of danhba
that danhba_id
, ho
, ten
danhba
danhba_id
, the name of the INTERSECT query will return those records.
For example - use ORDER BY
Use the ORDER BY clause with INTERSECT query to sort results.
SELECT nhacung_id, nhacung_ten
FROM nhacung
WHERE nhacung_id > 500
INTERSECT
SELECT congty_id, congty_ten
FROM congty
WHERE congty_ten in ('Apple', 'Microsoft', 'SQL Server')
ORDER BY 2;
Because the column name in the two SELECT statements is different, it is easier to reference the column in the ORDER BY clause by their position in the result set. In the above example, we filter the result nhacung_ten
/ congty_ten
in ascending order through the phrase ORDER BY 2.
Because the nhacung_ten
/ congty_ten
is the 2nd in the result set.
Previous article: UNION ALL operator in SQL Server
The following article: EXCEPT operator in SQL Server
You should read it
- Combine AND and OR conditions in SQL Server
- UNION ALL operator in SQL Server
- OR conditions in SQL Server
- SQL Server 2019 - Microsoft Relational Database Management System
- The difference between web server and app server
- Learn about the architecture of MS SQL Server
- Instructions for installing MS SQL Server
- New points in SQL Server 2017
May be interested
- INTERCEPT function - The function returns the point at which the line will intersect the y-axis by using the existing x and y values in Excelintercept function: the function returns the point at which the line will intersect the y-axis by using existing x and y values. use the function when you want to determine the value of a dependent variable when the independent variable is 0. syntax: intercept (known_ys, known_xs)
- AND conditions in SQL Serverin sql server, the and condition (or and operator) is used to test two or more conditions.
- Operator overload and Load overlap in C ++c ++ allows you to define more than one definition for a function name or an operator in the scope, respectively called load overloading (function overloading) and load operator overloading in c ++. .
- Load the Input / Output operator stack in C ++c ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators
- Operator overloading in C #overloading operator is operator overload. you can redefine or overload most existing operators in c #.
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...
- Stack operator in Pythonyou can change the meaning of the operator in python depending on the operand used and we call that operator overloading. quantrimang will learn more about this content through the article. invites you to read the track.
- OpenAI Announces Operator, an AI Agent That Automatically Performs Tasks on User's Behalfthe company behind chatgpt is unveiling a new way to use ai. openai has just announced operator, an ai agent that can automatically perform tasks for you.
- The difference between web server and app serveryou 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 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.