Table of Contents
MS SQL Server: INTERSECT Operator in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
In SQL Server (Transact-SQL), INTERSECT operator helps 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_thucnFROM bang[WHERE dieu_kien]INTERSECTSELECT bieu_thuc1, bieu_thuc2, … bieu_thucnFROM bang[WHERE dieu_kien];
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.
As an example, - with 1 expression
SELECT sanpham_idFROM sanphamINTERSECTSELECT sanpham_idFROM hangtonkho;
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_idFROM sanphamWHERE sanpham_id >= 50INTERSECTSELECT sanpham_idFROM hangtonkhoWHERE 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.
As an example, - with multiple expressions
SELECT danhba_id, ho, tenFROM danhbaWHERE ho = 'Anderson'INTERSECTSELECT nhanvien_id, ho, tenFROM nhanvien;
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.
As an example, - use ORDER BY
Use the ORDER BY clause with INTERSECT query to sort results.
SELECT nhacung_id, nhacung_tenFROM nhacungWHERE nhacung_id > 500INTERSECTSELECT congty_id, congty_tenFROM congtyWHERE congty_ten in ('Apple', 'Microsoft', 'SQL Server')ORDER BY 2;
FAQ
What should you know about iNTERSECT query?
Illustrate the result returned from INTERSECT query.
What is MS SQL Server: INTERSECT Operator in SQL Server?
In SQL Server (Transact-SQL), INTERSECT operator helps return records in both data sets or SELECT statements.
Why is MS SQL Server: INTERSECT Operator in SQL Server important?
A clear understanding of MS SQL Server: INTERSECT Operator in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Reader Comments 0
Sign in with email or Google to join the discussion.