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 1tqC8L-000yWN-KV for pgsql-novice@arkaria.postgresql.org; Thu, 06 Mar 2025 14:23:33 +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 1tqC8K-00DOQW-CE for pgsql-novice@arkaria.postgresql.org; Thu, 06 Mar 2025 14:23:32 +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 1tqC8K-00DODY-0p for pgsql-novice@lists.postgresql.org; Thu, 06 Mar 2025 14:23:32 +0000 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tqC8I-001JZb-0L for pgsql-novice@postgresql.org; Thu, 06 Mar 2025 14:23:31 +0000 Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-ab7430e27b2so121984666b.3 for ; Thu, 06 Mar 2025 06:23:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1741271009; x=1741875809; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=p/S9PONUvPefqIqkE68cEXlvw+/8ivCZ4YyDBU0NkHk=; b=YhvBo+9Ws+bNgEYMurN7eIHJOM3RLKSKB+Yr5hPj8q/ZSugiQDnEGl0LI+PUEcsfwo sQWUkPJJfRdczVlLdgInWrfv+bl/hB9KLZFtB8seooIoSfV5gNYQi93UHpkjnoKnW9Yc ZGotJ1qsagTTTRIR/j47BWS1FNw+N56du+m7+CHtzjqaVHjxocHetPaa/GPqUX8KG2iF zE6koDnG++eixkWnUj/Q4j+JQu9DEv63DaRIKqyeITJh1PqNaWEw7ggeviwf9Mo5Iage JYnkGVWdGOdkTR6KbP0XAkxYYBW28PhHQ/qXFRPvQdv3o9xRXB3BtXEoHhOprvemfFS4 rO0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741271009; x=1741875809; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=p/S9PONUvPefqIqkE68cEXlvw+/8ivCZ4YyDBU0NkHk=; b=YVha2gify5mToM8n7xrfEnYMJ3lHlX4IUosijNTtl0/liWiMD9MFeuOIqng8B2KyCQ BHJZ5XWSTB4CbNCtiKvpnsSw3IaranvHknsFUcyffkL458BgZYCjnUfjrjea6uwrx3tu 0rAs+3fLD4gDIF4evXBmQrQKuAw12Mmff4hbIn1rQy77lmCkhNIvYdggVI9m4Jm9fiTC CgIfzS0dtjUTmQB9PJ6MScfBxPgLS1DDJ4g/3IJ4qivY3gfeF1TOLWc8FuYLJQW27x2E cEfkNNxfVI2lLETK9ywRnnV4cJWHoM4H/PBPw384cQavdJvmiAMHIuiUwLDv/bFqnyCc 0DoA== X-Forwarded-Encrypted: i=1; AJvYcCXfmlX07PeVZjwG+Wzlrk9n0zDCFWsxcuRG6NoUvz9lRIQmavygyIDu3wbJCEwnb+s1VdJepnDUrKM7Yx8=@postgresql.org X-Gm-Message-State: AOJu0YwFuMx1kW7fDtOIk4wBc/qqCuUzsI6zcOvRPeJoj3XcN0vgkSF/ ySiaKKxQzXyLWkiCxFDaKnqYg46VuGfIYCITEKWjU3pX+mD6XpNa1L7OTVbsWR14vTXf1F/ngwg E X-Gm-Gg: ASbGnct58Y4B/AhztS9B/a7Osw8buXPtVMIcZ1TsS7UGucEtcEYze6HG4vlALCmmbr9 PWpKGe7hDSxHiU7rfF9foD12QQy8L1scptcrdydxpvK8+rE8JZARPdINWPx5JqIf2WGi45kuAwF sS9JtlXrAoBOfqewOEgWJ4dzaJC4F0C9HCK1v0Nj+1gGuuIHFsCDh3kNZGDLVdimu5ljJrn9Rvv 4vDNF+GR4GXwJ1U51/EIVdByezxsDrcFTH360cA5uZEZnn5y/gfodgnYUPa8Bus7KbXMXCW2Q3v 9y1J6EPFFVW44CU48BzFYjuyNgihg841/FxtE8LBGk8G95tRcBBeTVSJdaeJ5Q== X-Google-Smtp-Source: AGHT+IHXdVQRMJcHYgDh5olTpsO6BAPM3q90j8kUhEB/Mvx0jKNy70LT9STMft1UFAh/cZVID719JQ== X-Received: by 2002:a17:906:1542:b0:ac2:f8e:c8f1 with SMTP id a640c23a62f3a-ac20f8ec9a3mr645179166b.18.1741271008595; Thu, 06 Mar 2025 06:23:28 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:b59d:48a2:d9:55d2:155c]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac23943c476sm102547966b.3.2025.03.06.06.23.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Mar 2025 06:23:28 -0800 (PST) Message-ID: <35e4de19cd13bf1b2744fccf647b94e0e4476b61.camel@cybertec.at> Subject: Re: Subqueries From: Laurenz Albe To: Narsimham Chelluri , pgsql-novice@postgresql.org Date: Thu, 06 Mar 2025 15:23:27 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2025-03-05 at 15:01 -0400, Narsimham Chelluri wrote: > Can someone please explain to me why my first query does not return an er= ror?=20 >=20 > subquerytest=3D# select * from something where id in (select id); > =C2=A0id=20 > ---- > (0 rows) >=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. Right. > And that I would have to disambiguate if it did by using > aliases or table names preceding a dot. Right again. > 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 ref= er > only to values within its own specific from clause and error out otherwis= e? > Maybe I could do that with a CTE? I follow the following rule: Whenever an SQL statement refers to more than one table, qualify all column references with the table alias: select * from something where something.id in (select somethingelse.id); Yours, Laurenz Albe