Re: set difference

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Domoszlai László <dlacko(at)finit(dot)hu>
Cc: "SQL PostgreSQL (E-mail)" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: set difference
Date: 2002-10-15 14:25:39
Message-ID: 14808.1034691939@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

=?iso-8859-1?Q?Domoszlai_L=E1szl=F3?= <dlacko(at)finit(dot)hu> writes:
> I would like to make symmetrical(set) difference in a query.
> But the simpliest way I could find is

> select id from a
> except
> select id from b
> union
> select id from b
> except
> select id from a

> Is there any better solution for this problem?

One thing you should definitely do is change "union" to "union all".
"union" implies a pass of duplicate removal, which shouldn't be
necessary here (unless a or b individually contain duplicates and
you want to get rid of those too).

Another thing to try is
(a union b) except (a intersect b)
(Again, you might be able to say union all instead of union.)
Not sure which will be faster.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2002-10-15 16:28:12 Re: Messy Casts, Is there a better way?
Previous Message Jeff 2002-10-15 14:14:01 Re: how do i insert an empty string ?