Join two tables that do have same columns

PHOTO EMBED

Wed Jan 11 2023 01:46:53 GMT+0000 (Coordinated Universal Time)

Saved by @praveenms079 ##sql ##joins

# CREATE TABLE 1
CREATE TABLE table1 (
  id INT,
  col1 VARCHAR(255),
  col2 VARCHAR(255),
  col3 INT
);

# CREATE TABLE 2
CREATE TABLE table2 (
  id INT,
  col1 VARCHAR(255),
  col3 INT,
  col5 VARCHAR(255)
);

# INSERT VALUES TO TABLE 1
INSERT INTO table1(id, col1, col2, col3) 
VALUES (1, 'John', 'Doe', 25), (2, 'Jane', 'Smith', 32);

# INSERT VALUES TO TABLE 2
INSERT INTO table2(id, col1, col3, col5) 
VALUES (3, 'Alex', 30, 'US'), (4, 'Bob', 25, 'UK');

# JOIN BOTH TABLES SUCH THAT OUTPUT COLUMN AS COLUMNS FROM TABLE1 AND COLUMNS FROM TABLE2
SELECT T1.id, T1.col1, T1.col2, T1.col3, null as col5
FROM table1 T1 LEFT JOIN table2 T2 USING(ID)
union
SELECT T2.id, T2.col1, null as col2, T2.col3,T2.col5
FROM table1 T1 RIGHT JOIN table2 T2 USING(ID)
content_copyCOPY