From: saikiran mothe [mailto:saikiran.mothe@gmail.com]
Sent: Saturday, November 10, 2012 10:14 PM
To: pgsql-sql@postgresql.org
Subject: How to compare two tables in PostgreSQL
Hi,
How can i compare two tables in PostgreSQL.
Thanks,
Sai
Here is simple sql to show data in table1, but not in table2:
SELECT <common_column_list> from table1
EXCEPT
SELECT <common_column_list> from table2;
And this sql shows data in table2 but not in table1:
SELECT <common_column_list> from table2
EXCEPT
SELECT <common_column_list> from table1;
Or, you could combine them in one statement, adding "indicator" column:
SELECT <common_column_list>, 'not in table2' as indicator from table1
EXCEPT
SELECT <common_column_list>, 'not in table2' as indicator from table2
UNION
SELECT <common_column_list>, 'not in table1' as indicator from table2
EXCEPT
SELECT <common_column_list>, 'not in table1' as indicator from table1;
Regards,
Igor Neyman