title: SQL Filter Records On List of Ids
body: <p>I have the following junction table:</p>
  
  <p>PersonPhoto</p>
  
  <pre><code>+----+----------+---------+
  | Id | PersonId | PhotoId | 
  +----+----------+---------+
  |  1 |       10 |       5 |
  |  2 |       11 |       8 |
  |  3 |       12 |      28 |
  |  4 |       10 |      15 |
  |  5 |       10 |      28 |
  |  6 |       12 |      15 |
  +----+----------+---------+
  </code></pre>
  
  <p>I am trying to filter the table to only return PhotoId based on a list of PersonId's </p>
  
  <p>For example</p>
  
  <p>I would like to get all Photo Ids that have the users 10 and 12 associated with them. Using the table above that should return the following photos ids</p>
  
  <p>15 and 28..</p>
  
  <p>At the moment my efforts are returning 15, 28, 5 - However I don't want 5 to be returned because Person Id 12 is not associated with photo id 5</p>
  
  <p>Here's what I have tried already:</p>
  
  <pre><code>select distinct pe.PhotoId 
  from PersonPhoto AS pe
  where pe.PersonId IN (10, 12)
  GROUP BY pe.PhotoId
  
  select pp.PersonId, pp.PhotoId from PersonPhoto AS pp
  where pp.PersonId IN (10, 12)
  GROUP BY pp.PersonId, pp.PhotoId
  HAVING COUNT(DISTINCT pp.PhotoId) = 1
  </code></pre>
  
  <p>Any ideas?</p>