The difference between Union & Union All in SQL Server/pOSTGRESQL
Following is test in SQL Server:
USE [TestDB] CREATE TABLE [dbo].[UserInfoTest02]( [number] [bigint] NOT NULL, [name] [varchar](20) NOT NULL, [age] [int] NOT NULL, [sex] [bit] NOT NULL, [address] [varchar](200) NULL ) CREATE TABLE [dbo].[UserInfoTest03]( [number] [bigint] NOT NULL, [name] [varchar](20) NOT NULL, [age] [int] NOT NULL, [sex] [bit] NOT NULL, [address] [varchar](200) NULL ) CREATE TABLE [dbo].[UserInfoTest04]( [SID] [bigint] NOT NULL, [name] [varchar](20) NOT NULL, [age] [int] NOT NULL, [sex] [bit] NOT NULL, [address] [varchar](200) NULL ) Insert into UserInfoTest02 (number,name,age,sex,address) Values (1, 'A',12,0,'A'), (2, 'B',12,0,'A') Insert into UserInfoTest03 (number,name,age,sex,address) Values (1, 'A',12,0,'A'), (2, 'B',12,0,'BB') Insert into UserInfoTest04 (SID,name,age,sex,address) Values (1, 'A',12,0,'A'), (2, 'B',12,0,'BB')
SELECT SID, name,age,sex,address FROM UserInfoTest04 UNION ALL SELECT number,name,age,sex,address FROM UserInfoTest02
--Test Result1
SID name age sex address
1 A 12 0 A
2 B 12 0 BB
1 A 12 0 A
2 B 12 0 A
SELECT SID, name,age,sex,address FROM UserInfoTest04 UNION SELECT number,name,age,sex,address FROM UserInfoTest02
--Test Result2
SID name age sex address
1 A 12 0 A
2 B 12 0 A
2 B 12 0 BB
SELECT number, name,age,sex,address FROM UserInfoTest03 UNION SELECT number,name,age,sex,address FROM UserInfoTest02
--Test Result3
number name age sex address
1 A 12 0 A
2 B 12 0 A
2 B 12 0 BB
Test in Postgresql:
INSERT INTO "Test01"("SID", "Name") VALUES (1, 'A'); INSERT INTO "Test01"("SID", "Name") VALUES (2, 'B'); INSERT INTO "Test01"("SID", "Name") VALUES (3, 'C'); INSERT INTO "Test02"("SID", "Name") VALUES (1, 'A'); INSERT INTO "Test02"("SID", "Name") VALUES (2, 'B'); INSERT INTO "Test03"("Number", "address") VALUES(1, 'A'); INSERT INTO "Test03"("Number", "address") VALUES(2, 'B');
SELECT "SID", "Name" FROM "Test01" UNION ALL SELECT "SID", "Name" FROM "Test02";
--Test Result1:
1;"A"
2;"B"
3;"C"
1;"A"
2;"B"
SELECT "SID", "Name" FROM "Test01" UNION SELECT "Number", "address" FROM "Test03";
--Test Result2:
2;"B"
1;"A"
3;"C"