X-Original-To: pgsql-general@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 973784758C9 for ; Wed, 19 Feb 2003 10:14:31 -0500 (EST) Received: from wolff.to (wolff.to [66.93.249.74]) by postgresql.org (Postfix) with SMTP id C1327475843 for ; Wed, 19 Feb 2003 10:14:30 -0500 (EST) Received: (qmail 17618 invoked by uid 500); 19 Feb 2003 15:28:12 -0000 Date: Wed, 19 Feb 2003 09:28:12 -0600 From: Bruno Wolff III To: Mario Weilguni Cc: jerome@gmanmi.tv, pgsql-general@postgresql.org Subject: Re: SQL query... Message-ID: <20030219152812.GB17434@wolff.to> Mail-Followup-To: Mario Weilguni , jerome@gmanmi.tv, pgsql-general@postgresql.org References: <200302200035.37249.jerome@gmanmi.tv> <003b01c2d81c$3e716d20$8f01c00a@icomedias.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <003b01c2d81c$3e716d20$8f01c00a@icomedias.com> User-Agent: Mutt/1.3.25i X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/1136 X-Sequence-Number: 37890 On Wed, Feb 19, 2003 at 14:38:53 +0100, Mario Weilguni wrote: > >if i have 5 tables and all of it has column X and i wanted to get unique > >values in column X for all tables.. > > >can i do it in one query? > > sure. this is one way to do this: > select distinct foo.X from ( > select X from table1 > union > select X from table2 > .... > ) as foo; > > if the values are unique within every table, then you might want to omit the > surrounding select. Union's will already only return unique values. So you don't need the outer select distinct. You might get better performance by using union alls with select distinct, since only one sort will be needed. For the case where you know they are all unique, you can use union alls without the select distinct.