One method uses exists :
select distinct p.directorId from pantomine p where exists (select 1 from pantomine p2 where p2.directorId = p.directorId and p2.year = p.year + 1 );
There are other interesting options in this idea, such as using in :
select distinct p.directorId from pantomine p where p.year in (select p2.year + 1 from pantomine p2 where p2.directorId = p.directorId );
And here is a completely secret method that does not use the union mechanisms at all (just aggregation):
select distinct directorId from ((select directorId, year from pantomine) union all (select directorId, year + 1 from pantomine) ) p group by directorId, year having count(*) = 2;
This is also one of those really, very rare cases of using select distinct with group by .
source share