title: How do I select every value that corresponds to another value in another table in SQL
body: <p>The title may sound a little confusing but essentially what I am trying to do is I'm trying to pull from the table below. The query I used to create the table below was</p>
  
  <pre><code>    select d.FOLDER_ID, d.PKG_ID, p.file_id, d.PKG_START_TIME, d.PKG_END_TIME, d.ISVALID, d.VALIDFILE_COUNT, d.INVALIDFILE_COUNT, d.ISLOADED, d.LOADEDFILE_COUNT, d.REJECTEDFILE_COUNT 
  
  from DATA_EXCHANGE_PACKAGE d  full outer join PACKAGE_FILE p on d.PKG_ID = p.PKG_ID
  
  where d.pkg_id = p.pkg_id
  
  order by PKG_START_TIME asc
  </code></pre>
  
  <p>This table contains data from two different tables as you can see in the query and it selects the first records based on package start time. </p>
  
  <p>What I am trying to achieve is I want a query which can choose the amount of pkg_id's to return but I want every file_Id chosen for the amount of pkg_id chosen. For example, in my database I may have 100 packages but I only want to choose every file_Id  for the first 10 packages. How do I do this. I've only been able to choose the first 5 records using top and choose just 5 distinct pkg_id rows but not every file_id for those distinct 5 pkg_ID's. Any help would be appreciated. I understand group by and partition may work to achieve what I want but I haven't been successful. I'm not the greatest at SQL so this is why i'm struggling, I thought this query was going to be easier to create. I'm also  certain the where statement is pointless but I kept it regardless.</p>
  
  <p>Also let's assume the folder_Id is always 1.</p>
  
  <pre><code>+-----------+--------+---------+-------------------------+-------------------------+---------+-----------------+-------------------+----------+------------------+--------------------+
  | FOLDER_ID | PKG_ID | file_id |     PKG_START_TIME      |      PKG_END_TIME       | ISVALID | VALIDFILE_COUNT | INVALIDFILE_COUNT | ISLOADED | LOADEDFILE_COUNT | REJECTEDFILE_COUNT |
  +-----------+--------+---------+-------------------------+-------------------------+---------+-----------------+-------------------+----------+------------------+--------------------+
  |         1 |      1 |       1 | 2019-11-19 14:59:24.343 | NULL                    | NULL    | NULL            | NULL              | NULL     | NULL             | NULL               |
  |         1 |      2 |       2 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       3 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       4 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       5 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       6 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       7 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       8 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |       9 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |      10 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      2 |      11 | 2019-11-19 15:10:20.157 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      12 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      13 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      14 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      15 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      16 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      17 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      18 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      19 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      20 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  |         1 |      3 |      21 | 2019-11-19 15:58:26.733 | NULL                    | 1       | 10              | 0                 | NULL     | NULL             | NULL               |
  </code></pre>
  
  <p>An example of what I want to achieve with the above data is only want to choose the first two distinct pkg_id's based on the pkg_start_time in ascending order. However when only choosing those two distinct pkg_id's I want every file_id for those pkg_id's outputted. The below table is what I want my query to select from the above table.</p>
  
  <pre><code>+-----------+--------+---------+-------------------------+--------------+---------+-----------------+-------------------+----------+------------------+--------------------+--------+
  | FOLDER_ID | PKG_ID | file_id |     PKG_START_TIME      | PKG_END_TIME | ISVALID | VALIDFILE_COUNT | INVALIDFILE_COUNT | ISLOADED | LOADEDFILE_COUNT | REJECTEDFILE_COUNT | seqnum |
  +-----------+--------+---------+-------------------------+--------------+---------+-----------------+-------------------+----------+------------------+--------------------+--------+
  |         1 |      1 |       1 | 2019-11-19 14:59:24.343 | NULL         | NULL    | NULL            | NULL              | NULL     | NULL             | NULL               |      1 |
  |         1 |      2 |       2 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      1 |
  |         1 |      2 |       3 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      2 |
  |         1 |      2 |       4 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      3 |
  |         1 |      2 |       5 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      4 |
  |         1 |      2 |       6 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      5 |
  |         1 |      2 |       7 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      6 |
  |         1 |      2 |       8 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      7 |
  |         1 |      2 |       9 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      8 |
  |         1 |      2 |      10 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |      9 |
  |         1 |      2 |      11 | 2019-11-19 15:10:20.157 | NULL         | 1       | 10              | 0                 | NULL     | NULL             | NULL               |     10 |
  +-----------+--------+---------+-------------------------+--------------+---------+-----------------+-------------------+----------+------------------+--------------------+--------+
  </code></pre>
  
  <p><strong>Edit: I have solved my question</strong></p>