public inbox for [email protected]  
help / color / mirror / Atom feed
From: David G. Johnston <[email protected]>
To: Rich Shepard <[email protected]>
Cc: pgsql-general <[email protected]>
Subject: Re: Syntax error needs explanation
Date: Mon, 14 Jul 2025 13:07:53 -0700
Message-ID: <CAKFQuwZWnGB2weH87+z4zn8mTg83rso2Cy4P2=fqqcAM9p5iWg@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>

On Mon, Jul 14, 2025 at 12:59 PM Rich Shepard <[email protected]>
wrote:

>
> The current version of the script:
>
> select c.company_nbr, c.company_name, c.industry
> from companies as c
> where exists (
>    select e.company_nbr
>    from enforcement as e
>    )
> group by c.industry
> order by c.industry;
>
> And psql tells me that c.company_nbr must be in the group by clause.
> However, when I do that the output is a list of company numbers and names
> in
> each industry.
>
> My web searches on using the exists operator haven't provided the knowlege
> for me to use it properly.
>
>
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)
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;

David J.


view thread (6+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Syntax error needs explanation
  In-Reply-To: <CAKFQuwZWnGB2weH87+z4zn8mTg83rso2Cy4P2=fqqcAM9p5iWg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox