title: Taking N-samples from each group in PostgreSQL
body: <p>I have a table containing data that has a column named <code>id</code> that looks like below:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">id</th>
  <th style="text-align: center;">value 1</th>
  <th style="text-align: center;">value 2</th>
  <th style="text-align: left;">value 3</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">244</td>
  <td style="text-align: center;">550</td>
  <td style="text-align: left;">1000</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">251</td>
  <td style="text-align: center;">551</td>
  <td style="text-align: left;">700</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">540</td>
  <td style="text-align: center;">60</td>
  <td style="text-align: left;">1200</td>
  </tr>
  <tr>
  <td style="text-align: left;">...</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: left;">...</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">19</td>
  <td style="text-align: center;">744</td>
  <td style="text-align: left;">2000</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">10</td>
  <td style="text-align: center;">903</td>
  <td style="text-align: left;">100</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">44</td>
  <td style="text-align: center;">231</td>
  <td style="text-align: left;">600</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">120</td>
  <td style="text-align: center;">910</td>
  <td style="text-align: left;">1100</td>
  </tr>
  <tr>
  <td style="text-align: left;">...</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: left;">...</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I want to take 50 sample rows per <code>id</code> that exists but if less than 50 exist for the group to simply take the entire set of data points.</p>
  <p>For example I would like a maximum 50 data points randomly selected from <code>id = 1</code>, <code>id = 2</code> etc...</p>
  <p>I cannot find any previous questions similar to this but have tried taking a stab at at least logically working through the solution where I could iterate and union all queries by <code>id</code> and limit to 50:</p>
  <p><code>SELECT * FROM (SELECT * FROM schema.table AS tbl WHERE tbl.id = X LIMIT 50) UNION ALL;</code></p>
  <p>But it's obvious that you cannot use this type of solution because <code>UNION ALL</code> requires aggregating outputs from one id to the next and I do not have a list of <code>id</code> values to use in place of X in <code>tbl.id = X</code>.</p>
  <p>Is there a way to accomplish this by gathering that list of unique <code>id</code> values and union all results or is there a more optimal way this could be done?</p>