X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 161E3D1BA38 for ; Tue, 9 Mar 2004 05:32:30 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 19526-10 for ; Tue, 9 Mar 2004 01:32:28 -0400 (AST) Received: from localhost.localdomain (unknown [200.245.23.5]) by svr1.postgresql.org (Postfix) with ESMTP id 4D8EED1DB51 for ; Tue, 9 Mar 2004 01:32:14 -0400 (AST) Received: from yahoo.com.br (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.12.8/8.12.8) with ESMTP id i295fq3C003011 for ; Tue, 9 Mar 2004 02:41:59 -0300 Message-ID: <404D5920.40104@yahoo.com.br> Date: Tue, 09 Mar 2004 02:41:52 -0300 From: Daniel Henrique Alves Lima Reply-To: email_daniel_h@yahoo.com.br User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Help to simplify sample query Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/71 X-Sequence-Number: 17170 Hi guys, i have a new question about how to simplify a query. I have the tables area_course(cd_area,cd_course) and teacher_course(cd_teacher,cd_course) and a set of pairs of values {(1,2),(98,45),(11,0),...}. Now, i must to select the areas which courses appears in teacher_course and match with the set of pairs. Something like : select cd_area from area a1 where exists (select * from teacher_course c2 where c2.cd_course = a1.cd_course and c2.cd_teacher = 1 and c2.cd_course = 2) and exists (select * from teacher_course c2 where c2.cd_course = a1.cd_course and c2.cd_teacher = 98 and c2.cd_course = 45) and exists (select * from teacher_course c2 where c2.cd_course = a1.cd_course and c2.cd_teacher = 11 and c2.cd_course = 0) and .... This is just a sample. The whole query is giant and its use other tables/columns. Is there a best way to write this query ? I don't think this is possible, but: Is there a efficient way to compare if the result of a query is a superset of the result of another ? I would appreciate any ideas/suggestions. Thanks !!!