Re: Can't understand how a query from the documentation works

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sergey Samokhin <prikrutil(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Can't understand how a query from the documentation works
Date: 2009-09-08 19:40:11
Message-ID: 15529.1252438811@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Sergey Samokhin <prikrutil(at)gmail(dot)com> writes:
> The query I have a problem with is:

> SELECT * FROM fdt WHERE c1 IN (SELECT c1 FROM t2 WHERE c2 = fdt.c1 + 10);

> I've never seen column names being used inside subquries, so let me
> explain how I supposed it to work. It should help you to see where I'm
> making a mistake.

I think you're misunderstanding the behavior of the "outer reference"
fdt.c1. What will happen (at least conceptually) is that for each row
of fdt, the whole sub-SELECT will be evaluated using that row's value
of fdt.c1, producing a list of t2.c1 values. Then, if the fdt row's
value of c1 appears in that list, the fdt row will be included in the
result.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Wood 2009-09-08 22:47:26 Re: problem loading sql to database?
Previous Message Sergey Samokhin 2009-09-08 18:22:08 Can't understand how a query from the documentation works