Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 December PostgreSQL: Get the data difference between two Tables

PostgreSQL: Get the data difference between two Tables

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

Dec 3, 2017Anvesh Patel
PostgreSQL: Create a Copy of Table or Create a Duplicate TablePostgreSQL: Replace String data in all the Columns of a Table
Comments: 4
  1. Abhi
    December 3, 2017 at 4:26 pm

    Can I use left join to do the same thing here?

    ReplyCancel
  2. ikomrad
    January 18, 2019 at 8:09 pm

    Is this really the difference between 2 tables?
    Table 1
    a = 1
    b =2
    c = 3

    Table 2
    a = 5
    b = 2
    c= 3

    If I wanted to write a query that told me that A in Table 1 is not the same value as A in Table 2, how would I got about it?

    ReplyCancel
  3. Namdev Rathod
    April 22, 2019 at 1:02 pm

    Hi,

    Thanks for your post. I have one quick question here.

    I want to check difference between two tables from two different database like dev and prod.

    and if difference found from dev then update same to prod.
    only structure not data.

    Please help me with suitable solution and query.

    ReplyCancel
  4. leebert
    June 5, 2019 at 3:47 pm

    Food for thought … here’s a different way to look for inter-table diffs ..

    CREATE INDEX tbl_A_idx0 ON tbl_A (id) ;
    CREATE INDEX tbl_A_idx1 ON tbl_A ( ( tbl_A )::text) ;

    CREATE INDEX tbl_B_idx0 ON tbl_B (id) ;
    CREATE INDEX tbl_B_idx1 ON tbl_B ( ( tbl_B )::text) ;

    SELECT
    ( tbl_A)::text = ( tbl_B)::text

    FROM tbl_A a
    JOIN tbl_B b
    ON a.ID = b.ID
    AND ( tbl_A)::text ( tbl_B)::text ;

    (a more exotic approach, creating a data type … you have to ensure that tbl_A & tbl_B are both of type tbl_AB_type )

    CREATE TYPE tbl_AB_type ( id int, fld1 bigint, fld2 text ) ;
    CREATE INDEX tbl_A_idx1 ON tbl_A ( ( tbl_A )::tbl_AB_type ) ;
    CREATE INDEX tbl_B_idx1 ON tbl_B ( ( tbl_B )::tbl_AB_type ) ;

    SELECT
    ( tbl_A)::tbl_AB_type = ( tbl_B)::tbl_AB_type as _bool
    , ( tbl_A) AS _row_A
    , ( tbl_B) AS _row_B
    INTO TEMP TABLE _tmp_tbl
    FROM tbl_A a
    JOIN tbl_B b
    ON a.ID = b.ID
    AND ( tbl_A)::tbl_AB_type ( tbl_B)::tbl_AB_type ;

    SELECT (_row_A).id, (_row_B).fld1
    FROM _tmp_tbl
    WHERE ( _row_A) = (_row_B) ;

    … and so on …

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

December 3, 2017 4 Comments PostgreSQLAnvesh Patel, data compare, database, database research and development, dbrnd, Full Outer Join, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Monitoring, PostgreSQL Performance Tuning, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....