Re: How can we match a condition among 2 diff. tables?

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: <bhuvansql(at)yahoo(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How can we match a condition among 2 diff. tables?
Date: 2001-07-13 16:18:19
Message-ID: Pine.LNX.4.30.0107131816250.677-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

bhuvansql(at)yahoo(dot)com writes:

> Now, i need to get the details of all employees who did
> receive NONE of the salesorders. ie.. i wish to select the
> records of table 'employee' whose 'emp_id' are not
> there in table 'salesorder'.

SELECT * FROM employee WHERE emp_id NOT IN (SELECT emp_id FROM salesorder);

or, slightly uglier but possibly faster

SELECT * FROM employee WHERE NOT EXISTS
(SELECT 1 FROM salesorder WHERE salesorder.emp_id = employee.emp_id);

--
Peter Eisentraut peter_e(at)gmx(dot)net http://funkturm.homeip.net/~peter

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Matt Rinkenberger 2001-07-13 16:33:23 using custom column names in a group by clause
Previous Message Stephan Szabo 2001-07-13 15:43:54 Re: How can we match a condition among 2 diff. tables?