title: SQL Determine the teams having same members
body: <p>I have an interesting problem.</p>
  
  <p>I've some teams with a team leader stored in one table and the members of teams are stored in child table. I want to determine the teams that have same members. </p>
  
  <pre><code>TEAMS
  TEAM_ID    LEADER_ID
  1   1
  2   1
  3   2
  4   2
  
  MEMBERS
  TEAM_ID MEMBER_ID
  1   2
  1   3
  1   4
  2   3
  2   4
  2   5
  3   1
  3   3
  3   4
  4   5
  4   6
  4   7
  </code></pre>
  
  <p>I was able to write this query to determine the formations and now I am clueless how to proceed. </p>
  
  <pre><code>SELECT
      TEAM_ID,
      (
          SELECT
              CONVERT (VARCHAR, MEMBER_ID) + ', '
          FROM
              (
                  SELECT
                      TEAM_ID,
                      LEADER_ID AS MEMBER_ID
                  FROM
                      TEAMS
                  UNION ALL
                      SELECT
                          TEAM_ID,
                          MEMBER_ID
                      FROM
                          MEMBERS
              ) FORMATIONS
          WHERE
              TEAM_ID = MT.TEAM_ID
          ORDER BY
              MEMBER_ID FOR XML PATH ('')
      ) AS MEMBERS
  FROM
      TEAMS MT
  </code></pre>
  
  <p>As it is clear that team id 1 and 3 are same, how can the lowest ID of the duplicate teams can be obtained.</p>
  
  <p>i.e. the query should return the list of TEAM_IDs that are smallest per duplicate group (and only if they are duplicate)</p>
  
  <p>In this scenario id 1 should be returned.</p>
  
  <p><a href="http://sqlfiddle.com/#!18/c845a/5" rel="nofollow noreferrer">http://sqlfiddle.com/#!18/c845a/5</a></p>