Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id C43FB633CE0 for ; Fri, 9 Oct 2009 20:07:19 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 71412-04 for ; Fri, 9 Oct 2009 23:07:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from megazone.bigpanda.com (unknown [64.147.171.210]) by mail.postgresql.org (Postfix) with ESMTP id E917E63332F for ; Fri, 9 Oct 2009 20:07:11 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id C791C35884; Fri, 9 Oct 2009 16:06:56 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id C51C935739; Fri, 9 Oct 2009 16:06:56 -0700 (PDT) Date: Fri, 9 Oct 2009 16:06:56 -0700 (PDT) From: Stephan Szabo To: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= Cc: pgsql-sql@postgresql.org Subject: Re: Existential quantifier In-Reply-To: <86bpkgqiv8.fsf@ds4.des.no> Message-ID: <20091009160318.J80119@megazone.bigpanda.com> References: <86bpkgqiv8.fsf@ds4.des.no> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN Content-Transfer-Encoding: QUOTED-PRINTABLE X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.499 tagged_above=-10 required=5 tests=BAYES_00=-2.599, RDNS_NONE=0.1 X-Spam-Level: X-Archive-Number: 200910/35 X-Sequence-Number: 33234 On Sat, 10 Oct 2009, [utf-8] Dag-Erling Sm=C3=B8rgrav wrote: > Consider the attached schema (filmstars.sql), which is a poorly designed > database of films and actors. The following query gives me a list of > films in which either Charlie or Martin Sheen starred: > > select fs.film.title, fs.film.year > from fs.film left join fs.star on fs.film.id =3D fs.star.film > where fs.star.last =3D 'Sheen' > group by fs.film.title, fs.film.year; > > Is there a way to do this without the "group by" clause? Not at all tested as I don't have access to my db right now, but I think something like one of these would work: select fs.film.title, fs.film.year from fs.film where exists(select 1 from fs.star where fs.film.id =3D fs.star.film and fs.star.last =3D 'Sheen'); select fs.film.title, fs.film.year from fs.film where fs.film.id in (select fs.star.film where fs.star.last =3D 'Sheen');