Skip site navigation (1) Skip section navigation (2)

Re: Is not equal to query...

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: James David Smith <james(dot)david(dot)smith(at)gmail(dot)com>
Cc: pgsql novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Is not equal to query...
Date: 2011-06-09 16:27:50
Message-ID: BANLkTimR-gQxCHJ-5mirj7f_AHEhk7vuRA@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
On Thu, Jun 9, 2011 at 6:17 AM, James David Smith
<james(dot)david(dot)smith(at)gmail(dot)com> wrote:
> Hi Merlin,
>
> Thank you very much for the full and brilliant reply. The last query
> you wrote does exactly what I want it too. I wonder whether it's not
> too much trouble whether you could explain to me a couple of things
> though...?
>
> 1) I understand what you are saying my query does. Like an outer join
> I think? But don't get why. Using '!=' is the opposite of '=' is it
> not?

It is the opposite -- just not in the way you are thinking.  In SQL,
joins between tables means 'give me every combination of data from
table A combined with table B given a condition'.  If A and B each
have 100 records with identifiers 1-100, the not equal join would give
you a join result of 99 records for A=1 (with B 2-100), 99 records for
A=2 (with B 1, 3-100) etc. for a total of 9900 records.

Your problem is that you are still associating tables A and B in your
head in a way that is not expressed in the join.  You have to imagine
both tables as pools of unassociated records with no ordering except
for what you give in the query.

> 2) I understand the 'where not exists' query you suggest, and have
> used that, but I don't see why you use '1' in it. What does the 1 do?

where not exists means 'return this record if this query does not
return at least 1 record' -- since we don't care what is in the record
that is actually returned, I just use 1 as a shorthand because in SQL
you can't write queries that return 0 fields -- this is a pretty
common convention.  I instead of 1, I could have used *, -999, or
anything -- it doesn't matter.

Aside: exists/not exists in relational parlance are called 'semi
joins'.  They can often be faster than regular joins because the
server can bail early without having to express the full join.

merlin

In response to

pgsql-novice by date

Next:From: Vincent FicetDate: 2011-06-10 06:46:07
Subject: Indenting PLPGSQL code
Previous:From: Charles N. CharottiDate: 2011-06-09 16:10:10
Subject: Calling inner functions vs. Begin-End blocs

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group