Re: How to compare two tables in PostgreSQL

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: saikiran mothe <saikiran(dot)mothe(at)gmail(dot)com>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to compare two tables in PostgreSQL
Date: 2012-11-13 15:19:19
Message-ID: A76B25F2823E954C9E45E32FA49D70EC08EEEC02@mail.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


From: saikiran mothe [mailto:saikiran(dot)mothe(at)gmail(dot)com]
Sent: Saturday, November 10, 2012 10:14 PM
To: pgsql-sql(at)postgresql(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Louis-David Mitterrand 2012-11-17 16:19:17 organizing cron jobs in one function
Previous Message Allan Kamau 2012-11-12 10:44:22 Re: How to compare two tables in PostgreSQL