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 1tqAnE-000bPv-4M for pgsql-novice@arkaria.postgresql.org; Thu, 06 Mar 2025 12:57:40 +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 1tqAnC-00Ad7w-2D for pgsql-novice@arkaria.postgresql.org; Thu, 06 Mar 2025 12:57:38 +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 1tpu0h-002MK0-V0 for pgsql-novice@lists.postgresql.org; Wed, 05 Mar 2025 19:02:27 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tpu0g-0019p8-0C for pgsql-novice@postgresql.org; Wed, 05 Mar 2025 19:02:27 +0000 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-2feb91a2492so10133640a91.2 for ; Wed, 05 Mar 2025 11:02:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741201345; x=1741806145; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=amEecZN1a8pSCWn9UlNiY2IAeHFq4HU1MeLDEfy6YJ4=; b=R0Iqud07GLXP9urBXnRj3nll+ff8jxBO3HU2PkOHkeQvoA1coFBTLrSR7tfyc3RaZU T+fTQIOd2OVoZCGAnJL85PWz3CH+vYCXDYeLC85lvFy5heni+sqqxeY6vRdK/hBsRbZS zYfvXs77kupoGAm5hU0TizKiDQKy161vrn4Ia6q6ViQ9kKuy6Nxeerq3WIJt5s6gzAne SRZPE9nVpFcn2oF9yiAriAnYqLRHlWVP11Wj88zaAAI0W8lx9xsUONkI8lS7PyIp+tQU 2cAYNno8M09UgHycZ6TIMmSsLkoF4zBNsyANVpU0HCDElueVzyjpHLqgnMczhbgT3ff+ +0hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741201345; x=1741806145; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=amEecZN1a8pSCWn9UlNiY2IAeHFq4HU1MeLDEfy6YJ4=; b=SHmil2yUyG+3IaajH9eVC1itCsWhvUhAlVf6zfNoxquwsG7Fow9KK7TpD98sJgq0Qk Jm0zsSOm32Fc8BewmilnpbBd0EfKaFE3n1yv1w+OpBaBF9DVbmtILcnKGhdKf2YU+tzR ROlLXpZuDO589LGtAIC5htNS5KIHRSMf7pYFAaBo9sxNbgJwNT1yNRZNQJJsnlEd9gaG apMVgdsBDn/DGgfRfJfI9HiejhHS9xUgCVBC9x7sUvPsDtuZiEx5jxfxX/hkG0qbRBeC Zm9sHQuhw+KRpn4+SnsPpiRS9YD0+cJRSikQ5EEpTtQniiWjy+5pgRn0k95AS6AaRmON Vl/w== X-Gm-Message-State: AOJu0Yz9oGLQ88Q7OYfCnaYLANWsDtAmTmVXdpUxE6brszc2BoLwypHp pKl6aGh3pot0CHrsAdWAGMDM1FIhYPcd3WpdxFL7giTO65WVIFnRkPF2E3zJ/2RFHmAWeV2BREn /izNq9L9iw6c4KsG3pM/GeXO/VWAoFGat X-Gm-Gg: ASbGncs1MjEr2UskjgAve/HRAR6f1Od0Wos07NpS4Mo5r2C14uD8nONPFKGF/1b0znb OQKXSo+9XtqZaf9TKiQtpzXzUJmRdQxrng+fzZGxGK+Gu2Z6JCwK/qErIodKnhp4UZInDh2lPeQ +iDFmbzIJmmEyVzrfiWRtvoh9s X-Google-Smtp-Source: AGHT+IHmGtnfd7nBps+NR7omQLHyn8/RG7GRNWF84EnofyKDwulHmT79y8OO63A5r5mycd6pB21eew4kLFf1bi/QeHU= X-Received: by 2002:a17:90b:350a:b0:2ee:f80c:6884 with SMTP id 98e67ed59e1d1-2ff497cb056mr8001979a91.33.1741201345097; Wed, 05 Mar 2025 11:02:25 -0800 (PST) MIME-Version: 1.0 From: Narsimham Chelluri Date: Wed, 5 Mar 2025 15:01:49 -0400 X-Gm-Features: AQ5f1Jr27g6r2IqCvbqepcnXuZ8eXmkdO5kQZJ1dnrJt9rRoUhMG2fzl4N15F6E Message-ID: Subject: Subqueries To: pgsql-novice@postgresql.org Content-Type: multipart/alternative; boundary="000000000000a838b3062f9d071a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a838b3062f9d071a Content-Type: text/plain; charset="UTF-8" Hello, Can someone please explain to me why my first query does not return an error? -- subquerytest=# create table something (id bigint generated always as identity primary key); CREATE TABLE subquerytest=# create table somethingelse (asdf text); CREATE TABLE subquerytest=# select * from something where id in (select id from somethingelse); id ---- (0 rows) subquerytest=# select id from somethingelse; ERROR: column "id" does not exist LINE 1: select id from somethingelse; ^ subquerytest=# select * from something where id in (select id); id ---- (0 rows) subquerytest=# 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=# -- 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. 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? Thanks. - Narsa --000000000000a838b3062f9d071a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,

Can someone please ex= plain to me why my first query does not return an error?
--
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 someth=
ingelse);
 id=20
----
(0 rows)

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=20
----
(0 rows)

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# 
--

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 co= urse does not refer to "somethingelse" because that doesn't h= ave such a column on it. And that I would have to disambiguate if it did by= using aliases or table names preceding a dot.

If = that is correct: I almost made a mistake in a subquery where I used the wro= ng column in the subquery. Is it possible to make the subquery refer only t= o values within its own specific from clause and error out otherwise? Maybe= I could do that with a CTE?

Thanks.

- Narsa
--000000000000a838b3062f9d071a--