🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.vdbase.data.sql
Article #963

Re: SQL Select where CustNo Not in Table B

#963
From: Steve Koterski
Date: Mon, 08 Jun 1998 00:00
43 lines
2025 bytes
On 8 Jun 1998 05:47:34 GMT, "Tom Gleaton" <tgleaton@linkline.com> wrote:

>Table A & B each have a Customer No field.
>How would I write an SQL statement to select all rows in table A,
>that do not have a Customer Number in table B?

There are a number of ways to do that. One uses the IN predicate, negated
with the NOT logical operator.

  SELECT A."Customer No"
  FROM A
  WHERE (A."Customer No" NOT IN
    (SELECT DISTINCT B."Customer No"
    FROM B)
    )

Another way uses the EXISTS predicate, again negated by the NOT operator.

  SELECT A."Customer No"
  FROM A
  WHERE (NOT EXISTS
    (SELECT DISTINCT B."Customer No"
    FROM B
    WHERE (B."Customer No" = A."Customer No"))
    )

And still another way involves joining the tables in an outer join and then
filtering the data to just those joined rows where the B table key column
is empty (contains a NULL value), something only possible when a given row
in the A table does not have a match in the B table.

  SELECT DISTINCT A."Customer No"
  FROM A
    LEFT OUTER JOIN B
      ON (A."Customer No" = B."Customer No")
  WHERE (B."Customer No" IS NULL)

//////////////////////////////////////////////////////////////////////////
Steve Koterski                      "Knowledge comes, but wisdom lingers."
Technical Publications                -- Alfred, Lord Tennyson (1809-1914)
INPRISE Corporation
http://www.inprise.com/delphi
Technical Details
Message-ID: <35800f99.3088552@forums.inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bd92a1$9fba0440$384c96d1@dict2>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: ff5a1,6f0fb48f9c27fcb6
X-Google-Attributes: gidff5a1,public
From: koterski@borland.com (Steve Koterski)
Subject: Re: SQL Select where CustNo Not in Table B
Date: 1998/06/08
Message-ID: <35800f99.3088552@forums.inprise.com>
X-Deja-AN: 360714993
Content-Transfer-Encoding: 7bit
References: <01bd92a1$9fba0440$384c96d1@dict2>
Content-Type: text/plain; charset=us-ascii
Organization: INPRISE Corporation, Technical Publications
Mime-Version: 1.0
Reply-To: koterski@borland.com
Newsgroups: borland.public.vdbase.data.sql