The set operator is operated on two sets, two sets must have the same number of columns, the column has the same data type (at least implicitly convertible), and the final output of the collection's column name is determined by the column name of the first collection (which can be used to concatenate multiple results).
Note: Union "union" is not the same as join.
Union: Combines multiple result sets into a single result set. Union (removes duplicates, which is equivalent to applying distinct by default), and union all does not remove duplicates.
In general, we use union all, because to connect two result sets, of course, two result sets are required, the union all is more efficient than union, and less steps to remove duplicates.
The following two tables are used, Practice:
Example one:
Example two:
Example three:
Example four:
Example five:
Following these two lines of code after execution:
SELECT * FROM dbo. Student
SELECT * FROM dbo. Student
As can be seen, two SQL statements, respectively, detected three rows of records.
Then we change the SQL:
This is also the query out 3 records, the Union keyword, for us to remove the duplicate three records.
Keep looking, the following:
You can see the union all, the query out all the record lines, no removal of duplicates;
Summarize:
sql--Union query "union"