RE: Query will execute when inner query have issue

From: selva kumar <selva(dot)logic(at)hotmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: RE: Query will execute when inner query have issue
Date: 2020-01-23 15:52:12
Message-ID: MAXPR01MB2157CA93DACAD45CEBE77973E20F0@MAXPR01MB2157.INDPRD01.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Greetings

I did the following excise.

CREATE TABLE product(
productid int PRIMARY KEY,
productname CHARACTER VARYING NOT NULL);

INSERT INTO product values (1,'Pen');
INSERT INTO product values (2,'Pencil');

CREATE TABLE sales(
salesdate date,
product int,
qyt int);

INSERT INTO sales VALUES ('2020-01-22',1,10);
INSERT INTO sales VALUES ('2020-01-22',2,20);

select productid from sales where salesdate='2020-01-22';
ERROR: column "productid" does not exist
LINE 1: select productid from sales where salesdate='2020-01-22'

But the following query will executed
select * from product where productid in (select productid from sales where salesdate='2020-01-22')

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Sent: Thursday, January 23, 2020 8:13:33 PM
To: selva kumar <selva(dot)logic(at)hotmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Query will execute when inner query have issue

selva kumar <selva(dot)logic(at)hotmail(dot)com> writes:
> We tried query in a following manner.
> SELECT * FROM A where A.id IN (SELECT B.id from B);
> In the above query Table B does not have id. But this query return all A table records

You sure that's *actually* what you wrote? The usual mistake is to
fail to qualify the inner query's column reference at all:

SELECT * FROM A where A.id IN (SELECT id from B);

If B has no "id" column, it's still a legal SQL query, interpreted as

SELECT * FROM A where A.id IN (SELECT A.id from B);

so as long as B has any rows, the IN-test succeeds for every non-null
value of A.id. People get burned by that all the time, but it's
acting as required by the SQL standard.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2020-01-23 16:09:36 Re: BUG #16059: Tab-completion of filenames in COPY commands removes required quotes
Previous Message selva kumar 2020-01-23 15:51:56 RE: Query will execute when inner query have issue