How to display records in table programmatically?
In order to achieve this you need to do the following


Inject the database service as db_query is not working in Drupal 8 anymore
Use of theme() was discouraged in Drupal 7 as well, instead you need to use render array #theme key
Use Drupal 8 Database API db_select and chaining stuff and extend a special class Drupal\Core\Database\Query\TableSortExtender for rendering the data in tabular form
Finally form a render array with #theme key, #header, #rows. 


The following example from my custom module display the data in table along with pager

      // Prepare _sortable_ table header
      $header = array(
        array('data' =&gt; t('ID'), 'field' =&gt; 'id'),
        array('data' =&gt; t('Name'), 'field' =&gt; 'name'),
        array('data' =&gt; t('Email'), 'field' =&gt; 'field1'),
        array('data' =&gt; t('Age'), 'field' =&gt; 'field2', 'sort' =&gt; 'desc'),
        array('data' =&gt; t('Gender'), 'field' =&gt; 'field3'),
        array('data' =&gt; t('Operations'), 'field' =&gt; 'field4'),
      );

      $query = $this-&gt;database-&gt;select('location','loc');
      $query-&gt;fields('loc', array('id', 'name', 'field1','field2','field3','field4'));
      $query-&gt;addField('loc','name','name_alias');
      $table_sort = $query-&gt;extend('Drupal\Core\Database\Query\TableSortExtender')-&gt;orderByHeader($header);
      $pager = $table_sort-&gt;extend('Drupal\Core\Database\Query\PagerSelectExtender')-&gt;limit(10);
      $result = $pager-&gt;execute();

      foreach($result as $row) {
          $rows[] = array('data' =&gt; (array) $row);
      }


      $build = array(
          '#markup' =&gt; t('List of All locations')
      );

      $build['location_table'] = array(
        '#theme' =&gt; 'table', '#header' =&gt; $header,
          '#rows' =&gt; $rows
      );
     $build['pager'] = array(
       '#type' =&gt; 'pager'
     );