How to find the first message between 2 parties in SQL?
<p>I have a messages table that looks something like this:</p>

<pre><code>|   id  | sender_id |  recipient_id |
|-------------------|---------------| ...
|   1   |    23     |       20      |  
|   2   |    11     |       5       | ...
|   3   |    20     |       23      |
|   4   |    23     |       20      | ...
|   5   |    7      |       11      |
</code></pre>

<p>I'm hoping to find the first message between any two user IDs (the IDs in the <code>sender_id</code> and <code>recipient_id</code> columns). So the result for the above sample would be:</p>

<pre><code>|   id  | sender_id |  recipient_id |
|-------------------|---------------| ...
|   1   |    23     |       20      |  
|   2   |    11     |       5       | ...
|   5   |    7      |       11      |
</code></pre>

<p>At first I thought I could group by a checksum of <code>sender_id</code> and <code>recipient_id</code>, and then take the min message ID (<code>id</code>), but because checksum is different depending upon order of the inputs, that returns both the first message (the intro) and the first reply. Is there an alternative to checksum in which order of inputs is irrelevant?</p>

<p>Or maybe there's a better way to arrive at a solution.</p>

<p>Any help is much appreciated.</p>