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 1ubPYA-000TTi-Me for pgsql-general@arkaria.postgresql.org; Mon, 14 Jul 2025 20:13:22 +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 1ubPY8-00DIWr-4q for pgsql-general@arkaria.postgresql.org; Mon, 14 Jul 2025 20:13:20 +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 1ubPY7-00DIWX-Pj for pgsql-general@lists.postgresql.org; Mon, 14 Jul 2025 20:13:20 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1ubPY6-007I4g-1s for pgsql-general@postgresql.org; Mon, 14 Jul 2025 20:13:19 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id A75B92A14D6 for ; Mon, 14 Jul 2025 13:13:17 -0700 (PDT) Date: Mon, 14 Jul 2025 13:13:17 -0700 (PDT) From: Rich Shepard To: pgsql-general Subject: Re: Syntax error needs explanation [RESOLVED] In-Reply-To: Message-ID: References: <9c84776d-d938-9fa4-ab1-6ee960ce6e7d@appl-ecosys.com> <7ec33517-6dc1-4e1d-b741-0c26eba7cc79@aklaver.com> <6684eaa7-e6e2-a45b-cff9-44597ca3c8ad@appl-ecosys.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 14 Jul 2025, David G. Johnston wrote: > Yeah, you need both to read up on aggregate queries and correlated > subqueries which is typically how one makes uses of exists (it's called a > semi-join in this formulation) David, Thanks. I wasn't sure what to read. > Not tested, but: > > select c.industry, count(*) > from companies as c > where exists ( > select from enforcement as e > where e.company_nbr = c.company_nbr > ) > group by c.industry; Works as intended. Many thanks, Rich