This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a simple way to get the data difference between two tables in PostgreSQL.
Recently, one of the junior folk asked me about this and I prepared the query using FULL OUTER JOIN.
Using this solution, you can get the missing data for both tables in the form of differences.
Create two sample tables:
1 2 3 4 5 |
CREATE TABLE tbl_A (ID INT); CREATE TABLE tbl_B (ID INT); |
Insert sample data into both tables:
1 2 3 4 5 6 7 |
INSERT INTO tbl_A VALUES (1),(2),(3); INSERT INTO tbl_B VALUES (1),(4),(5); |
Check the difference of data:
1 2 3 4 5 6 7 |
SELECT a.id as tbl_B_Missing ,b.id as tbl_A_Missing FROM tbl_A a FULL OUTER JOIN tbl_B b ON a.ID = b.ID WHERE a.ID IS NULL OR b.ID IS NULL |
Result:
1 2 3 4 5 6 |
tbl_b_missing | tbl_a_missing --------------------------------- 2 | [null] 3 | [null] [null] | 4 [null] | 5 |