-
Notifications
You must be signed in to change notification settings - Fork 0
/
UNIONS_Intermediate_SQL.sql
50 lines (37 loc) · 1.23 KB
/
UNIONS_Intermediate_SQL.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
CREATE TABLE WareHouseEmployeeDemographics
( EmployeeID int,
FirstName varchar (50),
LastName varchar (50),
Age int,
Gender varchar (50))
SELECT *
FROM WareHouseEmployeeDemographics;
INSERT INTO WareHouseEmployeeDemographics VALUES
(1013,'Darryl','Philbin',NULL,'Male'),
1050,'Roy', 'Anderson', 31,'Male'),
(1051,'Hidetoshi','Hasagawa', 40,'Male'),
(1052,'Val', 'Johnson',31,'Female')
SELECT *
FROM EmployeeDemographics
FULL OUTER JOIN WareHouseEmployeeDemographics
ON EmployeeDemographics.EmployeeID = WareHouseEmployeeDemographics.EmployeeID
-- UNION EXCLUDES DUPLICATES
SELECT *
FROM EmployeeDemographics
UNION
SELECT *
FROM WareHouseEmployeeDemographics
-- UNION ALL INCLUDES DUPLICATES OR DUPLICATED ROWS REGARDLESS IF IT IS A DUPLICATE OR NOT
-- BUT THEY SHOULD BE THE SAME DATA TYPE OR RELATED FIELDS OR ELSE THE JOIN FUNCTION IS WAY MORE BETTER
SELECT *
FROM EmployeeDemographics
UNION ALL
SELECT *
FROM WareHouseEmployeeDemographics
ORDER BY EmployeeID
--- EVEN WHEN THE TABLES DON'T HAVE THE SAME NUMBERS OF COLUMNS YOU CAN CHOOSE WHICH FIELDS TO SELECT
SELECT EmployeeID, FirstName, Age
FROM EmployeeDemographics
UNION
SELECT EmployeeID, JobTitle, Salary
FROM EmployeeSalary