How to display records in table programmatically? I'm using Drupal 8.0.1. I'm starting to develop a new custom module for learning purpose. Just I have created one form. From the form, the values are stored in my custom table. Now I want to show the records in separate page. 

In drupal 7, I have used

  $query = db_query('SELECT id,name,email,age,gender FROM {learning}');
  $rows = array();
  while ($data = $query-&gt;fetchAssoc()) {
      // Fill the table rows
    $data['operation'] = l(t('Delete'), 'admin/people/survey/' . $data['id'] . '/delete');
    $data['operation'] .= "|";
    $data['operation'] .= l(t('Edit'), 'admin/people/survey/' . $data['id'] . '/edit');
    $rows[] = array('data' =&gt; $data , 'style' =&gt; 'word-break:break-all;' , );
  }
  //Number of records shown in per page
  $per_page = 4;
  $current_page = pager_default_initialize(count($rows), $per_page);
  $chunks = array_chunk($rows, $per_page, TRUE);
  // Prepare table header
  $header = array(t('ID'), t('Name'), t('Email'), t('Age'), t('Gender'), t('Operations'));
  // Output of table with the paging
  $output = theme('table',
    array(
      "header" =&gt; $header,
      "rows" =&gt; $chunks[$current_page],
      "attributes" =&gt; array(),
      "sticky" =&gt; TRUE,
      "caption" =&gt; "",
      "colgroups" =&gt; array(),
      "empty" =&gt; t("No Survey Records has been found")
    )
  );
  //return pager with limited number of records.
  return $output .= theme('pager', array('quantity', count($rows)));;


And this was working fine in drupal 7. I just want to do same in the Drupal 8. When I put this code in drupal 8 it shows error theme() not found. How can I create the table to show the list of records from my custom table.
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'
     );