Symmetric Pairs

  • + 1 comment
    /*
    I think this is a bit easier to understand 
    for somebody who prefers joins over exists 
    */
    select
        t.x
      , t.y
    from (
      select
          a.x
        , a.y
        -- , b.x as xi
        -- , b.y as yi
      from functions a
        inner join functions b
          on a.x = b.y
             and a.y = b.x
             and a.x < a.y
      union
      select
          x
        , y
        -- , count(*)
      from functions a
      where 1=1
        and x=y
      group by
        x,y
      having count(*) > 1
    ) t
    order by
       t.x
      ,t.y
    ;