After much effort I found another solution to my own problem which is better for generating
the query in C#.
Also I had a last_name column added so it complicated things a bit when i used intersect.
So I ended up with this
SQL Fiddle
Query 1:
DECLARE @TABLE TABLE(Id INT, first_name VARCHAR(20), department VARCHAR(20))
INSERT INTO @TABLE VALUES
(1 ,'John', 'IT'),
(2 ,'George', 'Support'),
(3 ,'Jack', 'IT'),
(4 ,'Jack', 'IT'),
(5 ,'George', 'Dev'),
(6 ,'Maria', 'Dev'),
(7 ,'George', 'IT'),
(8 ,'Maria', 'Support')
SELECT y.Id,y.first_name,y.department
FROM @table y
INNER JOIN (SELECT first_name, COUNT(*) AS CountOf, COUNT(distinct department) as CountDep
FROM @table
WHERE department in ('IT','Dev')
GROUP BY first_name
HAVING COUNT(*) > 1 and COUNT(distinct department) > 1
) dt ON y.first_name=dt.first_name
and department in ('IT','Dev')
order by first_name
Results:
| ID | FIRST_NAME | DEPARTMENT |
|----|------------|------------|
| 5 | George | Dev |
| 7 | George | IT |
Cause I pass the search parameters from a checkbox list its easier to manipulate the departments and the number of them.Furthermore if I want to find duplicates in one department all i have to do is put >0 in the COUNT department section