Re: Non Matching Records in Two Tables

From: Patrick JACQUOT <patrick(dot)jacquot(at)anpe(dot)fr>
To:
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Non Matching Records in Two Tables
Date: 2006-02-09 08:59:55
Message-ID: 43EB048B.6050203@anpe.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Ken Hill wrote:

> I need some help with a bit of SQL. I have two tables. I want to find
> records in one table that don't match records in another table based
> on a common column in the two tables. Both tables have a column named
> 'key100'. I was trying something like:
>
> SELECT count(*)
> FROM table1, table2
> WHERE (table1.key100 != table2.key100);
>
> But the query is very slow and I finally just cancel it. Any help is
> very much appreciated.
>
> -Ken

Maybe you could use a NOT EXISTS subquery, as in
SELECT count(*) from table1
WHERE NOT EXISTS(SELECT count(*) from table2 WHERE table1.key100
=table2.key100)
which gives you the number of records in table1 without corresponding
records in table2.
That kind of query is quite fast, if there exists an index on table2.key100
hth
P.Jacquot

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Philippe Lang 2006-02-09 09:35:03 Concatenate strings using GROUP BY
Previous Message george young 2006-02-09 07:44:50 Re: unique constraint instead of primary key? what