title: TSQL Query Where All Records Must Exists to Return A Record
body: <p>I am not sure even how to ask this question.</p>
  
  <p>I have a table of tags:</p>
  
  <pre><code>TagId  Tag
  -----  -----
  1      Fruit
  2      Meat
  3      Grain
  </code></pre>
  
  <p>I have a table of events:</p>
  
  <pre><code>EventId   Event
  -------   -----------
  1         Eating Food
  2         Buying Food
  </code></pre>
  
  <p>What I need to do is bring back only Events that have all selected tags associated with it.</p>
  
  <p>If three tags are selected then only show event that have all three.</p>
  
  <p>For example:</p>
  
  <p>Mapping Table</p>
  
  <pre><code>EventId   TagId
  -------   -----
  1         1
  1         3
  2         1
  </code></pre>
  
  <p>If I write a query like this:</p>
  
  <pre><code>select * from MapTable where where tagId in (1,3)
  </code></pre>
  
  <p>This will return Eating Food and Buying Food.</p>
  
  <p>But what I need to do is bring back events that have both tags 1 and 3.  This means that the only event in this case I would return would be Eating Food as it has both selected tags.</p>
  
  <p>I was wondering if this can be done in TSQL or if I will have to use the business layer to translate it into the object to return back to the GUI.</p>
  
  <p>Thanks.</p>