title: Inner Join a Table to Itself
body: <p>I have a table that uses two identifying columns, let's call them id and userid. ID is unique in every record, and userid is unique to the user but is in many records.</p>
  
  <p>What I need to do is get a record for the User by userid and then join that record to the first record we have for the user. The logic of the query is as follows:</p>
  
  <pre><code>SELECT v1.id, MIN(v2.id) AS entryid, v1.userid
  FROM views v1
  INNER JOIN views v2
    ON v1.userid = v2.userid
  </code></pre>
  
  <p>I'm hoping that I don't have to join the table to a subquery that handles the min() piece of the code as that seems to be quite slow.</p>