I have a SQL query that I cant wrap my mind around. I do not have a large amount of sql experience. So I need some help
I have a table XXX:
Social Security No (SSN).
Name.
organisation. (Finance/IT)
In english what I want is:
To select all SSNs and Names in “Finance” where there is a different name for that SSN in “IT”.
My not working attempt:
select ssn , name from XXX where org = “Finance” and name not in (select name from XXX where org=”IT” and ssn=the_first_ssn)
Please help.
I have decided to make it a bit more difficult.
SSN can ocur multiple times in “IT”:
So I want to select all SSNs and Names in Finance where the SSN does not exist with the same Name in “IT”
You could use a subquery in an
existsclause:The subquery says there must be a row in IT with the same SSN but a different name.