Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g3N32-00069k-Hz for pgsql-sql@arkaria.postgresql.org; Fri, 21 Sep 2018 15:08:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g3N2z-0001hB-I8 for pgsql-sql@arkaria.postgresql.org; Fri, 21 Sep 2018 15:08:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g3N2z-0001h3-B4 for pgsql-sql@lists.postgresql.org; Fri, 21 Sep 2018 15:08:45 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g3N2x-0000j8-7K for pgsql-sql@lists.postgresql.org; Fri, 21 Sep 2018 15:08:44 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtp (Exim 4.90_1) (envelope-from ) id 1g3N2v-0001uX-8l for pgsql-sql@lists.postgresql.org; Fri, 21 Sep 2018 16:08:41 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: "pgsql-sql@lists.postgresql.org" Subject: select where not in () fails Date: Fri, 21 Sep 2018 16:08:41 +0100 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201809211608.41086.gary.stainburn@ringways.co.uk> X-Spam-Score: -50.6 (--------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: I have a users table with u_id as primary key. I have an employee record table with emp_u_id is a foreign key back to the users table. A user may have zero or more employee records (leaves then returns / changes department). An employee may have zero or one user record [...] Content analysis details: (-50.6 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.1 SCORE_RCPTS Adding score for each recipient 0.1 SCORE_RCPTS Adding score for each recipient -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0001] 0.1 AWL AWL: Adjusted score from AWL reputation of From: address 1.0 RING_SAFE No description available. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I have a users table with u_id as primary key. I have an employee record table with emp_u_id is a foreign key back to the users table. A user may have zero or more employee records (leaves then returns / changes department). An employee may have zero or one user record The select I am trying to get working to so be able to list all users without an employee record. Straight forward right????? Can anyone see why user record 2212 doesn't appear in the last select statement? users=# select count(u_id) from users; count ------- 716 (1 row) users=# select count(emp_u_id) from employees; count ------- 345 (1 row) users=# select count(*) from employees; count ------- 388 (1 row) users=# select emp_u_id from employees where emp_u_id=2212; emp_u_id ---------- (0 rows) users=# select u_id from users where u_id=2212; u_id ------ 2212 (1 row) users=# select count(u_id) from users where u_id in (select distinct emp_u_id from employees); count ------- 323 (1 row) users=# select count(u_id) from users where u_id not in (select distinct emp_u_id from employees); count ------- 0 (1 row)