Here's a sample graph:
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("managerId",123).as("manager").
......1> addV("employee","A").as("A").
......2> addV("employee","B").as("B").
......3> addV("department", "IT").as("it").
......4> addV("department", "Sales").as("sales").
......5> addE("manages").from("manager").to("A").
......6> addE("manages").from("manager").to("B").
......7> addE("worksFor").from("A").to("it").
......8> addE("worksFor").from("B").to("it").
......9> addE("worksFor").from("A").to("sales").iterate()
In this case, I make it so that employee A is in both "Sales" and "IT", but employee B is only in "IT". Since you said you wanted employees who work in both departments employee A is who should be returned from the query and B should be filtered out.
Note that the use of within
yields an incorrect answer in that case:
gremlin> g.V().has('managerId',123).
......1> out('manages').
......2> where(out('worksFor').
......3> has('department',within('IT','Sales'))).
......4> valueMap()
==>[employee:[A]]
==>[employee:[B]]
Here is the approach if you want both departments:
gremlin> g.V().has('managerId',123).
......1> out('manages').
......2> where(out('worksFor').
......3> has('department','Sales')).
......4> where(out('worksFor').
......5> has('department','IT')).
......6> valueMap()
==>[employee:[A]]