How to create a controller that runs query?
@asrob,
Please refer the basic docs provided by Clive to create a controller in drupal 8 with route to access the same or 
this link to create a custom module in drupal
and follow this sample code below to display your data in table format along with a pager:

  &lt;?php

  /**
   * @file
   * Contains \Drupal\custom_module_name\Controller\DetailsController
   */

  namespace Drupal\custom_module_name\Controller;

  use Drupal\Core\Controller\ControllerBase;

  /**
   * Description of DetailsController
   *
   * @author aaditya.walawalkar
   */
  class DetailsController extends ControllerBase {

    const RECORDS_PER_PAGE = 10;

    /**
     * {@inheritdoc}
     */
    public function getRecords() {

      $records = $query_result_from_query;

      // total records
      $totalRecords = $total_records_count_from_query;
      $recordsPerPage = self::RECORDS_PER_PAGE;
      // initialize pager
      $page = pager_default_initialize($totalRecords, $recordsPerPage);

      $offset = $recordsPerPage * $page;

      $data = array();
      foreach ($records as $delta =&gt; $details) {
        $data[] = array('data' =&gt; array(
          $details['name'],
          $details['remarks'],
        ));
      }

      $recordsToDisplay = [];
      $recordsToDisplay[] = [
        '#theme' =&gt; 'table',
        '#header' =&gt; array($this-&gt;t('Name'), $this-&gt;t('Remarks')),
        '#rows' =&gt; $data,
        '#empty' =&gt; $this-&gt;t('No Records(s) Found.'),
      ];

      // Finally, add the pager to the render array, and return.
      $recordsToDisplay[] = ['#type' =&gt; 'pager'];
      return $recordsToDisplay;

    }
  }