EXCEPT operator in SQL Server
The EXCEPT operator in SQL Server is used to return the rows in the first SELECT statement that are not returned in the second SELECT statement. Each SELECT statement will have a data set. EXCEPT operator takes the record from the 1st set and removes the results from set 2.
EXCEPT query
EXCEPT query illustration
Explanation: EXCEPT query returns records in the blue area, only in data set 1 and not in data set 2.
Each SELECT statement in the EXCEPT query must have the same number of fields in the result set with the same data type.
EXCEPT operator syntax
SELECT bieu_thuc1thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE dieu_kien]
EXCEPT
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 EXCEPT operator returns all records from the first SELECT statement and not in the second SELECT statement.
- EXCEPT operator in SQL Server is equivalent to MINUS operator in Oracle.
For example - with 1 expression
SELECTsanpham_id
FROM sanpham
EXCEPT
SELECT sanpham_id
FROM hang
tonkho;
In the example with this EXCEPT operator, the result returns all the sanpham_id values in the table of variables and not in the hangtonkho table. This means that if the sanpham_id value is available on both tables, it will not be returned.
For example - with multiple expressions
SELECT danhba_id, ho, ten
FROM danhba
WHERE ho = 'Anderson'
EXCEPT
SELECT nhanvien_id, ho, ten
FROM nhanvien
;
In this example, the query returns the records in the namba table with the contact ID, the first and last names do not match the employee's ID, last name, and first name in the table.
For example - use the ORDER BY clause
SELECT nhacung_id, nhacung_ten
FROM nhacung
WHERE bang = 'Florida'
EXCEPT
SELECT congty_id, congty_ten
FROM congty
WHERE congty_id <= 400
ORDER BY 2;
In this example, because the column name in the two SELECT statements is different, it is easier to refer to the column by the ORDER BY clause through the 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: INTERSECT operator in SQL Server
The following article: Query SUBQUERY child in SQL Server
You should read it
May be interested
- 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.
- Wildcard representation operator in Accesswildcard, also known as a wildcard, can be used to determine the location of a specific item when you don't remember exactly how it was written.