title: Select n rows from a table per distinct value of a column
body: <p>I have a huge data base that I want to sample based on one column. 
  This column have 10 distinct values, and for each value I want to select 5 rows from the data base. </p>
  
  <p>I've tried this query that works, but is very slow once the data becomes bigger.</p>
  
  <pre><code>SELECT *
  FROM table AS A
  WHERE rowid IN (SELECT rowid FROM table WHERE value=A.value LIMIT 5)
  
  </code></pre>
  
  <pre><code>Input:
  
  SOURCE  DATE            VALUE
  
  IG  2018-12-31  ONE
  IG  2019-05-24  TWO
  IG  2018-12-31  THREE
  TW  2019-06-24  SIX
  FB  2019-06-24  FIVE
  FB  2019-06-24  FIVE
  FB  2019-06-24  ONE
  FB  2019-06-24  THREE
  FB  2019-06-18  FIVE
  FB  2019-06-18  SIX
  FB  2019-06-18  ONE
  FB  2019-06-18  ONE
  IG  2019-06-24  TWO
  FB  2019-05-24  THREE
  IG  2019-06-24  THREE
  RD  2019-06-24  TWO
  RD  2019-06-24  SIX
  RD  2019-06-24  ONE
  TW  2019-06-24  FIVE
  FB  2019-06-24  TWO
  
  =============
  
  Output:
  
  SOURCE  DATE            VALUE
  
  FB  2019-06-24  ONE
  FB  2019-06-18  ONE
  IG  2019-05-24  TWO
  RD  2019-06-24  TWO
  FB  2019-05-24  THREE
  IG  2019-06-24  THREE
  FB  2019-06-24  FIVE
  FB  2019-06-24  FIVE
  FB  2019-06-18  SIX
  RD  2019-06-24  SIX
  </code></pre>