Re: Non Matching Records in Two Tables - Mailing list pgsql-sql

From Patrick JACQUOT
Subject Re: Non Matching Records in Two Tables
Date
Msg-id 43EB048B.6050203@anpe.fr
Whole thread Raw
In response to Non Matching Records in Two Tables  (Ken Hill <ken@scottshill.com>)
List 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


pgsql-sql by date:

Previous
From: george young
Date:
Subject: Re: unique constraint instead of primary key? what
Next
From: "Philippe Lang"
Date:
Subject: Concatenate strings using GROUP BY