title: Counting Child Rows As a Condition in a Query
body: <p>I have a table of Groups that have a "capacity" column and has a has_many relationship with Enrollments. I want to be able to find Groups whose count of enrollments is less than it's capacity, so using ActiveRecord + Ruby I can do this:</p>
  
  <pre><code>Group.all.select {|g| g.enrollments.count &lt; g.capacity }.first
  </code></pre>
  
  <p>But it seems like there should be a way to do this in SQL, I just don't know how. Any ideas?</p>