1

My table people_jobs:

+--------+--------+
|  NAME  |  JOB   | 
+--------+--------+
|  John  |  Actor | 
+--------+--------+
|  Jane  | Driver | 
+--------+--------+
|  Bill  |  Actor | 
+--------+--------+
|  John  |  Cook  | 
+--------+--------+

I'm looking to select all names with the job actor where the name column would be unique. The desired query output here would be just Bill.

Something like:

SELECT name FROM people_jobs WHERE job LIKE "actor" AND COUNT(SELECT * FROM people_jobs WHERE name LIKE name) = 1;

This is apparently bad syntax and I couldn't get GROUP BY to work... Thoughts?

6
  • SELECT distinct name FROM people_jobs WHERE job='actor' Something like that? Commented Sep 14, 2015 at 21:27
  • This still returns both John AND bill. Commented Sep 14, 2015 at 21:30
  • That's because john and bill are both actors. You need to rephrase your question. I understand now you want to select all actors with names that are present exactly once in the table. Commented Sep 14, 2015 at 21:32
  • You mean who are JUST Actor, without second job, right? Commented Sep 14, 2015 at 21:36
  • @CollinD The question is phrased appropriately. Commented Sep 14, 2015 at 21:37

6 Answers 6

3

Why a subselect?

SELECT name, COUNT(*) AS cnt
FROM people_jobs
WHERE job='Actor'
GROUP BY name
HAVING cnt = 1

Ok, now I see the problem. Try this instead:

SELECT name, SUM(JOB='Actor') AS actor_cnt, COUNT(*) as job_cnt
FROM people_jobs
GROUP BY name
HAVING (actor_cnt = 1) AND (job_cnt = 1)

This'll figure out how many people are actors, and count how many jobs they have, and return ONLY the people whose only job is acting.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm still getting both John and Bill back from this query.
hmm. and now I see the problem. hang on. modified version coming right up.
Perfect! I was hoping the answer wasn't THAT easy.
How would you convert this query to an update statement so that you were updating the name?
3
select name
from people_jobs
where name in (
  select name
  from people_jobs
  group by name
  having count(name) = 1
) and job like 'actor'

This example is simple for understanding, but I like another one:

SELECT name
FROM people_jobs
GROUP BY name
HAVING COUNT(name) = 1 AND sum(job = 'Actor') = 1

1 Comment

Can you add some explanation on how this works? Otherwise, good answer!
1
SELECT DISTINCT x.* 
           FROM people_jobs x 
           LEFT 
           JOIN people_jobs y 
             ON y.name = x.name 
            AND y.job <> x.job 
          WHERE x.job = 'actor' 
            AND y.name IS NULL;

1 Comment

Thanks for your input, Strawberry. I got a useable solution below, but am definitely giving you an upvote.
1
SELECT name, COUNT(*) c 
FROM people_jobs 
WHERE job = 'Actor' 
GROUP BY name HAVING c = 1

2 Comments

Should this not be GROUP BY name?
Yes, that was a typo that I just caught. Fixed.
0

I would use NOT EXISTS on this context:

SELECT name
FROM   people_jobs
WHERE  job = 'actor'
       AND NOT EXISTS (SELECT * FROM people_jobs pj
                       WHERE pj.name = people_jobs.name AND pj.job != 'actor')

Comments

-1

Easier and solve your problem:

SELECT distinct name FROM people_jobs WHERE job = "Actor"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.