title: SQLite query with two tables to find the "next episode to watch"
body: <p>So I have two tables, one is for TV Shows and the other for Episodes, schematically:</p>
  
  <pre><code>show {
    long id;
    String title;
  }
  
  episode {
   int season;
   int episode;
   long showId (foreign key)
   int watched: Default (0)
  }
  </code></pre>
  
  <p>I am trying to run a query that will return for each show, <strong>the next episode the user has to watch</strong>. Let's say I have two shows, Show A and Show B, each line is a season and each number an episode. I am representing the episodes with 0 (not watched) and 1 (watched).</p>
  
  <pre><code>A (2 seasons of 4 episodes)
  0000
  0000
  
  B (3 seasons of 3 episodes)
  000
  000
  000
  </code></pre>
  
  <p>For the previous example, the query should return:</p>
  
  <pre><code>A, season = 1, episode = 1
  B, season = 1, episode = 1
  </code></pre>
  
  <p>More examples:</p>
  
  <pre><code>A
  010
  000
  B
  111
  000
  </code></pre>
  
  <p>Here the query should return:</p>
  
  <pre><code>A, season = 1, episode = 3
  B, season = 2, episode = 1
  </code></pre>
  
  <p>Another:</p>
  
  <pre><code>A
  0100100101
  0001000000
  B
  1110000001
  0000000010
  </code></pre>
  
  <p>Here the query should return:</p>
  
  <pre><code>A, season = 2, episode = 5
  B, season = 2, episode = 10
  </code></pre>
  
  <p>As you can see, user can skip episodes, or watch them without order but I always want to show as "next to watch" the one that is immediately next to the furthest episode watched.</p>
  
  <p>Is it possible to do this in one query? (can have subqueries, of course). But I am looking for one query here so then I can attach it to an Android Loader.</p>