Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 7BDDB2E0044 for ; Mon, 21 Apr 2008 10:49:01 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 30365-04 for ; Mon, 21 Apr 2008 10:48:37 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by developer.postgresql.org (Postfix) with ESMTP id B46632E0035 for ; Mon, 21 Apr 2008 10:48:44 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1JnwNl-0005HC-Li for pgsql-sql@postgresql.org; Mon, 21 Apr 2008 13:48:33 +0000 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 21 Apr 2008 13:48:33 +0000 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 21 Apr 2008 13:48:33 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Having a mental block with (self) outer joins Date: Mon, 21 Apr 2008 15:48:23 +0200 Lines: 37 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080213 Thunderbird/2.0.0.12 Mnenhy/0.7.5.666 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.145 tagged_above=0 required=5 tests=AWL=-0.454, RCVD_IN_DNSWL_LOW=-1, RCVD_NUMERIC_HELO=2.599 X-Spam-Level: * X-Archive-Number: 200804/123 X-Sequence-Number: 30662 Hi, I'm playing around with putting a hierarchy of items into the database. But for some reason I'm having a mental block understanding the following: I have a table category with id and parent_id implementing the typical adjacency model. To get the first two levels of the hierarchy I use: SELECT t1.name as lev1, t2.name as lev2 FROM category t1 LEFT JOIN category t2 ON t2.parent = t1.id WHERE t1.name = 'ROOT' ; Now what I don't understand is that the root node (which has a NULL parent_id) is not selected. My understanding from the outer join is that it would return all items from the "left" tables regardless whether they have a corresponding row in the "right" table. So given the data name, id, parent_id ROOT, 1, NULL CHILD1, 2, 1 CHILD2, 3, 1 I would have expected the following result: ROOT, NULL ROOT, CHILD1 ROOT, CHILD2 but the row with (ROOT,NULL) is not returned. I'm sure I'm missing something very obvious, but what? Thanks in advance Thomas