title: Does ORDER BY apply before or after DISTINCT?
body: <p>In a MySQL query, when using the <code>DISTINCT</code> option, does <code>ORDER BY</code> apply after the duplicates are removed? If not, is there any way to make it do so? I think it's causing some issues with my code.</p>
  
  <p><strong>EDIT</strong>:<br>
  Here's some more information about what's causing my problem. I understand that, at first glance, this order would not be important, since I am dealing with duplicate rows. However, this is not entirely the case, since I am using an <code>INNER JOIN</code> to sort the rows.</p>
  
  <p>Say I have a table of forum threads, containing this data:</p>
  
  <pre class="lang-none prettyprint-override"><code>+----+--------+-------------+
  | id | userid |    title    |
  +----+--------+-------------+
  |  1 |      1 | Information |
  |  2 |      1 | FAQ         |
  |  3 |      2 | Support     |
  +----+--------+-------------+
  </code></pre>
  
  <p>I also have a set of posts in another table like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>+----+----------+--------+---------+
  | id | threadid | userid | content |
  +----+----------+--------+---------+
  |  1 |        1 |      1 | Lorem   |
  |  2 |        1 |      2 | Ipsum   |
  |  3 |        2 |      2 | Test    |
  |  4 |        3 |      1 | Foo     |
  |  5 |        2 |      3 | Bar     |
  |  6 |        3 |      5 | Bob     |
  |  7 |        1 |      2 | Joe     |
  +----+----------+--------+---------+
  </code></pre>
  
  <p>I am using the following MySQL query to get all threads, then sort them based on the latest post (assuming that posts with higher ids are more recent:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT t.*
  FROM Threads t
  INNER JOIN Posts p ON t.id = p.threadid
  ORDER BY p.id DESC
  </code></pre>
  
  <p>This works, and generates something like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>+----+--------+-------------+
  | id | userid |    title    |
  +----+--------+-------------+
  |  1 |      1 | Information |
  |  3 |      2 | Support     |
  |  2 |      1 | FAQ         |
  |  3 |      2 | Support     |
  |  2 |      1 | FAQ         |
  |  1 |      1 | Information |
  |  1 |      1 | Information |
  +----+--------+-------------+
  </code></pre>
  
  <p>However, as you can see, the information is correct, but there are duplicate rows. I'd like to remove such duplicates, so I used <code>SELECT DISTINCT</code> instead. However, this yielded the following:</p>
  
  <pre class="lang-none prettyprint-override"><code>+----+--------+-------------+
  | id | userid |    title    |
  +----+--------+-------------+
  |  3 |      2 | Support     |
  |  2 |      1 | FAQ         |
  |  1 |      1 | Information |
  +----+--------+-------------+
  </code></pre>
  
  <p>This is obviously wrong, since the "Information" thread should be on top. It would seem that using <code>DISTINCT</code> causes the duplicates to be removed from the top to the bottom, so only the final rows are left. This causes some issues in the sorting.</p>
  
  <p>Is this the case, or am I analyzing things incorrectly?</p>