title: Apply WHERE after GROUP BY
body: <p>I have a table with items that are part of a thread, so they have a thread ID, each item also has a date (UNIX timestamp). My table looks something like the following (UNIX timestamps simplified):</p>
  <pre><code>+-----------------------------+
  |  id |   date  |  thread_id  |
  +-----+---------+-------------+
  |  1  |   1111  |      4      |
  |  2  |   1333  |      4      |
  |  3  |   1444  |      5      |
  |  4  |   1666  |      5      |
  +-----------------------------+
  </code></pre>
  <p>What I want to do is select thread <code>IDs</code> where ALL the items sharing the same thread ID are smaller than the passed date. So if I wanted thread <code>IDs</code> where ALL items are older than <code>1555</code> (<code>date &lt; 1555</code>), I would only expect to have thread <code>ID 4</code> returned, not <code>5</code> even though it's got an item with a date smaller than <code>1555</code>. This is what I tried:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM table WHERE date &lt; 1555 GROUP BY thread_id ORDER BY date DESC
  </code></pre>
  <p>What I'm trying to do with that query is group all items by thread ID around the item with the highest date value and from there get the items where the date is less than <code>1555</code>. But that doesn't work, it will still return thread <code>ID 5</code> because it's got an item older than <code>1555</code>.</p>
  <p>So to summarize, how would I only select thread <code>IDs</code> where ALL items are older than a certain date?</p>