Carlos Delgado Carlos Delgado - 6 months ago 10
SQL Question

Exclude a row after returning it from a subquery mysql

Hi this is what I need: "Display the name and salary of all employees working on the same Department than 'Mike'. The result shouldn't include 'Mike' "

So I have this query:

SELECT E.Name,E.Salary FROM E WHERE E.DepWorking = (SELECT DepWorking FROM E WHERE Name = 'Mike');


and I get all the employees working in the same department than Mike but it is showing Mike's row too and I don't want that, how can I avoid Mike to show up??

Answer

And an additional condition in the WHERE clause:

SELECT 
    E.Name,E.Salary 
FROM E 
WHERE 
    E.DepWorking IN (SELECT DepWorking FROM E WHERE Name = 'Mike')
    AND E.Name <> 'Mike'
;