title: How do I select unique pairs of rows from a table at random?
body: <p>I have two tables like these:</p>
  
  <pre><code>CREATE TABLE people (
      id INT NOT NULL,
      PRIMARY KEY (id)
  )
  
  CREATE TABLE pairs (
      person_a_id INT,
      person_b_id INT,
      FOREIGN KEY (person_a_id) REFERENCES people(id),
      FOREIGN KEY (person_b_id) REFERENCES people(id) 
  )
  </code></pre>
  
  <p>I want to select pairs of people at random from the people table, and after selecting them I add the randomly select pair to the pairs table.  person_a_id always refers to the person with the lower id of the pair (since the order of the pair is not relevant).</p>
  
  <p>The thing is that I never want to select the same pair twice, so I need to check the pairs table before I return my randomly selected pair.</p>
  
  <p>Is it possible to do this using just a single SQL query in a reasonably efficient and elegant manner?</p>
  
  <p>(I'm doing this using the Java Persistence API, but hopefully I'll be able to translate any answers into JPA code)</p>