There may be a more efficient way to do this but this seems to work:;with UserRoles (UID, RID) as (select 'U1', 'R1' union allselect 'U1', 'R2' union allselect 'U1', 'R3' union allselect 'U2', 'R1' union allselect 'U2', 'R2' union allselect 'U3', 'R1' union allselect 'U3', 'R2' union allselect 'U3', 'R3' union allselect 'U4', 'R1' union allselect 'U4', 'R4' union allselect 'U4', 'R2' union allselect 'U4', 'R3')select a.[uid]from userRoles ajoin userRoles b on b.[uid] != a.[uid] and b.rid = a.ridwhere b.[uid] = 'U1'group by a.[uid]having count(*) >= (select count(*) from userRoles where [uid] = 'U1')OUTPUT:uid----U3U4
Be One with the OptimizerTG