Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqBqe-000toZ-7F for pgsql-novice@arkaria.postgresql.org; Thu, 06 Mar 2025 14:05:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tqBqc-00Cosu-Mf for pgsql-novice@arkaria.postgresql.org; Thu, 06 Mar 2025 14:05:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqBqc-00Cosm-8s for pgsql-novice@lists.postgresql.org; Thu, 06 Mar 2025 14:05:14 +0000 Received: from mout.gmx.net ([212.227.17.22]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tqBqa-001JQQ-08 for pgsql-novice@lists.postgresql.org; Thu, 06 Mar 2025 14:05:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.com; s=s31663417; t=1741269909; x=1741874709; i=lazyvirus@gmx.com; bh=eh0SnkzpGOmBy0UibSOpjM01sauFoqeahtZjJ7c6C1I=; h=X-UI-Sender-Class:Date:From:To:Subject:Message-ID:In-Reply-To: References:Content-Type:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=fG13Ir9YGttAFgQhJgJkCG19xQkaWr+pgs3AjuM74n7Ra1SsG3D9wveHQXRiVLnH P+eAZWLTaxbeNKAuv6cE/ib4zT6pxKrmoQIakxmF8RpGHDRUDIUcwVpx7wu9JEpMP 0yvipQFWvTa44zZl/ijlbOusGN1FSA11tjkHWBVA/A1+MoN+MAhcwqF1ZYxWXkihs OJ1gb+K5kgqDLZIb5mDopdqqL3BAxJB1afH5cTSzQuOHhqjwgFtLKNjZvRilguJEQ QIegh3fStqS++gCdW59rrujXx1ZPcqb1OPqOA7afaxPmV40CX6CI+U/iigRYRp49t HP9AQD1wzGhvjgbuSA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from mail.defcon1.lan ([93.15.31.113]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MPXdC-1tdcnv2CXq-00OIWz for ; Thu, 06 Mar 2025 15:05:09 +0100 Date: Thu, 6 Mar 2025 15:05:06 +0100 From: Bzzzz To: pgsql-novice@lists.postgresql.org Subject: Re: Subqueries Message-ID: <20250306150506.5b06d9df@msi> In-Reply-To: References: Organization: Anyone, anywhere but in banana demokratik republik of france or uerSS. Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:v9Fo1BY4kGJM7Hr8C2rj3TsepKgAeoUZVSbJW8Dhp59NNTldBgY MXghSeqquMO1ww264lOez3Aa4b8PEhG5tnhQZl6AvFyn1zIhcsajBqgMNmeQPa8NMDFQP8+ WpQqsv2lzbbl2+xgmZZNEXAbDU3BWp4uMzGtwfQm6zSpucsNDxEI5gvmtb5kDff6LFR54Rj csXF+Hf37tEwVmhbMwwCg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:0J3s22QK+ZM=;qw4+YU7vPCVVGYgkyjzCTnc2RbL nGnUOytsiclNu+7muMIj5tubsJSnlu2QXDBffWnsRR7OzXxb8x8duKKnr1AiaOHTwNhwK3iqY nNcp19r4CpgcvwPOiDoqwzjD7Gh8DrUvIeQCHUDLv8KY+Np8RQoUNcG5Wl0sYK/Bfx62e10VX QQ4M+7iIpsW22bwpeUbp2O8B6/B6WxAXosQSWwEJQaJvrr7zi0BBlxuHyxickFNyjHleN8wvI TYtmps88QBCA9UsDQRAuiPVsj30mzVlQqziVFH5FFj1Pmcw2PUF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13ev1fJjJVVCzSx5joB8+DuZrzYznyhnnzyxqMZLES7TGbW wPNlrkyomjk/ZL7SWOfVbXKeAyCXJhr7bYe9xC2twkBaGb64LYfHK6UHCt2HdlHFe42X0fbpm TnNURQ1I2snst9WJlPZ1uajPepi2JUM+Ry+oKf2tkQTRtKvn/ List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 5 Mar 2025 15:01:49 -0400 Narsimham Chelluri wrote: > Hello, >=20 > Can someone please explain to me why my first query does not return an > error? >=20 > -- >=20 > subquerytest=3D# create table something (id bigint generated always as > identity primary key); > CREATE TABLE > subquerytest=3D# create table somethingelse (asdf text); > CREATE TABLE > subquerytest=3D# select * from something where id in (select id from > somethingelse); > id > ---- > (0 rows) >=20 > subquerytest=3D# select id from somethingelse; > ERROR: column "id" does not exist > LINE 1: select id from somethingelse; > ^ > subquerytest=3D# select * from something where id in (select id); > id > ---- > (0 rows) >=20 > subquerytest=3D# select * from something where id in (select asdf); > ERROR: column "asdf" does not exist > LINE 1: select * from something where id in (select asdf); > ^ > subquerytest=3D# >=20 > -- >=20 > I would imagine it has something to do with: "from something" means > that "id" is available in the subquery and refers to the column in > "something" and of course does not refer to "somethingelse" because > that doesn't have such a column on it. And that I would have to > disambiguate if it did by using aliases or table names preceding a > dot. Hi, First, you did not said which query you were talking about in the text just above=E2=80=A6 Second, the absence of error comes from the 'in' statement, as the 2nd query involved by 'in' doesn't return anything, it doesn't fail the whole query, but indicates that there is no solution to it, hence the zero rows answer (you'll find this behavior addressed in the SQL standard IIRC). Third, you defined the PK of table 'something' as an automated value, which will trigger an error when trying to insert into table 'something' directly - you can of course override that, but consequences could be terrible if you don't know exactly what you are doing and why, so avoid that at all cost. > If that is correct: I almost made a mistake in a subquery where I > used the wrong column in the subquery. Is it possible to make the > subquery refer only to values within its own specific from clause and > error out otherwise? Maybe I could do that with a CTE? What are you talking about ?? You can't assume that YOU want a relationship between both tables without TELLING the DB engine that it is the case=C2=A0; PostgreSQL is excellent, but not to the level it reads your mind to build its table relationships ;-p) If you want to _tie_ one table column to another located in another table, you _must_ use a foreign key constraint, which will _enforce_ the link presence, something like : CREATE TABLE somethingelse (id bigint generated always as identity primary key); CREATE TABLE something (id bigint not null REFERENCES somethingelse(id)); This way, you won't be able to insert into table 'something' if the value doesn't already exist into table 'somethingelse'. This is one of the magic of databases, they take care of your mandatory predicates without a worry. Another thing is you use 'bigint' in one table and 'text' in the other, at the very least you should cast the text to a bigint, and as good practice never use such a hack as it is an open door to difficulties in the process of exploiting your database (when the text will not be able to be casted to a bigint, it'll raise an error you will have to localize and cure, which can easily cascade into inextricable modifications). IF (and ONLY IF) you are adamantine that the text value into table 'somethingelse' will _always_ be a 'bigint', then cast it to a 'bigint' _before_ insertion (and of course change the 'adsf' type from text to bigint). Jean-Yves --=20