title: Selecting items from a sequence with a minimum difference
body: <p>Let's say we have a table like this:</p>
  
  <pre><code>id | num
  ---+----
   1 | 1
   2 | 3
   3 | 5
   4 | 6
   5 | 7
   6 | 8
  </code></pre>
  
  <p>What I need to do is select all rows such that, when viewed as a sequence, the minimum difference between one row in the result and the next one is at least 3. The desired result should be this:</p>
  
  <pre><code>id | num
  ---+----
   1 | 1
   3 | 5
   6 | 8
  </code></pre>
  
  <p>This is trivial in imperative language:</p>
  
  <pre><code>result = [table[0]]
  for row in table:
      if row['num'] &gt;= result[-1]['num'] + 3:
          result.append(row)
  </code></pre>
  
  <p>But I'm struggling to find a solution in pure SQL (I'm using SQLite, btw).</p>
  
  <p>I did find a way which looked like this:</p>
  
  <pre><code>WITH RECURSIVE
    seq(t) as (
      SELECT 1
      UNION
      SELECT min(num) FROM table, seq WHERE num &gt;= t + 3 AND num &lt; 10
    )
  SELECT t FROM seq;
  </code></pre>
  
  <p>But this only worked in a specific version of SQLite, with a later version giving the error that aggregate functions ("min" in this case) aren't allowed in recursive queries.</p>
  
  <p>What I thought would work is this:</p>
  
  <pre><code>WITH RECURSIVE
    seq(t) as (
      SELECT 1
      UNION ALL
      SELECT num FROM table, seq WHERE num &gt;= t + 3 LIMIT 1
    )
  SELECT t FROM seq;
  </code></pre>
  
  <p>But in this case, <code>LIMIT 1</code> is applied to the whole subquery, not just the second <code>SELECT</code>, so that in the end, <code>seq</code> only has one row.</p>
  
  <p>Can this be done?</p>