title: Count rows returned by subquery
body: <p>Let's say I have this table named <code>tblsample</code></p>
  
  <p><img src="https://i.stack.imgur.com/m2Sxw.png" alt="enter image description here"></p>
  
  <p>I'm trying to get the number of <code>codes</code> unique to each <code>title</code>. So the result should be <code>abc = 3</code> and <code>def = 3</code></p>
  
  <p>My query right now is</p>
  
  <pre><code>Select T1.title, COUNT(tbl2.code) as [No. of Codes]
  from (Select t2.title, T2.CODE from tblsample as T2 where T2.category = 'com' group by t2.title, t2.CODE) AS tbl2
  inner join tblsample as T1 on (T1.title = tbl2.title)
  where T1.category = 'COM'
  group by T1.title
  order by T1.title
  </code></pre>
  
  <p>But it's returning <code>abc = 15</code> and <code>def = 9</code> so I'm quite confused. Where am I wrong here?</p>