views:

219

answers:

5

I was just playing with adventureworks database in sqlserver. I got stuck in a query. I wanted to Select all titles from HumanResources.Employee which are either 'Male' or 'Female' but not both. i.e if title Accountant is Male and Female both I want to leave that title. I need only those titles where Gender is either Male or Female.

I have done this till yet.

select distinct(title) from humanresources.employee where gender='M'
select distinct(title) from humanresources.employee where gender='F'

Probably a join between these two queries, would work. But If you have any other solution, please let me know.

It is not a homework. :)

Thanks in advance.

+2  A: 

Use:

SELECT t.title
  FROM HUMANRESOURCES.EMPLOYEE t
 WHERE t.gender = 'M'
   AND NOT EXISTS(SELECT NULL
                    FROM HUMANRESOURCES.EMPLOYEE e
                   WHERE e.gender = 'F'
                     AND e.title = t.title)
UNION ALL
SELECT t.title
  FROM HUMANRESOURCES.EMPLOYEE t
 WHERE t.gender = 'F'
   AND NOT EXISTS(SELECT NULL
                    FROM HUMANRESOURCES.EMPLOYEE e
                   WHERE e.gender = 'M'
                     AND e.title = t.title)
OMG Ponies
thanks for pointing out the the distinct titles issue. I didn't pick that up. Removing my response.
Arthur Thomas
Your query is returning 60 such 'Titles'. :) Please Check it. It is not the correct query.
vaibhav
The number of titles means nothing - you'll have to provide more information about what issue(s) you believe you have with the expected results.
OMG Ponies
A: 

Try this

select distinct title FROM humanResources.employee 
WHERE gender = 'M'  and 
  jobtitle not in (select title FROM humanResources.employee  WHERE gender='F') 
union
select distinct title FROM humanResources.employee 
WHERE gender = 'F'  and 
  jobtitle not in (select title FROM humanResources.employee  WHERE gender='M')
Sparky
This would only get the jobs where the titles were all male, but would not work for female.
Irwin M. Fletcher
@Irwin: Patience...
OMG Ponies
Just trying to encourage people to post correct solutions.
Irwin M. Fletcher
@Irwin: Then I hope you reverse downvotes once things have been corrected.
OMG Ponies
Sorry, I hit the wrong button before I completed the query
Sparky
Actually I tried after it was changed but it said it was too late.
Irwin M. Fletcher
A: 
SELECT DISTINCT(title)
  FROM HumanResources.Employee
 WHERE t.gender = 'M'
EXCEPT
SELECT DISTINCT(t.title)
  FROM HumanResources.Employee
 WHERE t.gender = 'F'

UNION

SELECT DISTINCT(title)
  FROM HumanResources.Employee
 WHERE t.gender = 'F'
EXCEPT
SELECT DISTINCT(t.title)
  FROM HumanResources.Employee
 WHERE t.gender = 'M'
womp
Most will/should have a knee-jerk reaction to say "don't use DISTINCT when you're using UNION", but it's necessary for the EXCEPT comparison.
OMG Ponies
Your query seems to be correct. but I am not able to get 'vice president of production' in resultset, It should be there.
vaibhav
+1  A: 

Here is the corrected version

select title from HumanResources.Employee as t
where gender='M' And Not Exists(select null from HumanResources.Employee as e
         where gender='F' And e.title =t.title)
Union 

select title from HumanResources.Employee as t1
where gender='F' And Not Exists(select null from HumanResources.Employee as e1
         where gender='M' And e1.title =t1.title)
order by title
Shantanu Gupta
A: 

select title from (select distinct title, gender from HumanResources.Employee) as temptable group by title having count(title) = 1

Klaas