1

i have this table:

Job(j_no,position,company,salary);

and I want to get all the positions that only one company offers. What I was trying to do was:

SELECT position, COUNT(*) AS total FROM (SELECT * FROM Job) GROUP BY 
position;

and i get for example:

  **position          / total**
  Director          |   1
  Draw              |   1
  Software Engineer |   2
  electrician       |   2

how can I return only the position that have a 1 total?

i try to do:

 SELECT position, COUNT(*) AS total FROM (SELECT * FROM Job) GROUP BY 
 position WHERE total=1;

but i get error.

Would appreciate help, thank's.

Ayed Mohamed Amine
  • 587
  • 2
  • 10
  • 30
Liran
  • 37
  • 1
  • 4

2 Answers2

2

Change WHERE with HAVING:

SELECT position, COUNT(*) AS total 
FROM Job 
GROUP BY position 
HAVING total = 1;
forpas
  • 160,666
  • 10
  • 38
  • 76
2

I want to get all the positions that only one company offers.

If you specifically want positions at only one company, then this is not where the total is 1. A single company could have multiple rows for the same position. So:

SELECT position, COUNT(*) AS total
FROM Job
GROUP BY position
HAVING MIN(company) = MAX(company);

If you wanted the detail on such positions, one method is `not exists:

select j.*
from job j
where not exists (select 1
                  from job j2
                  where j2.position = j.position and j2.company <> j.cmopany
                 )
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 2
    This might not matter for OP's data, but this answer is IMHO more correct for what is asked. Otherwise the question should be "all the positions which are offered only once". – Paul Spiegel Feb 23 '19 at 14:15