How to choose between classic SQL query and Drupal 8 technic? I have a custom content entity (amap_distributions_inscriptions) linked to 2 entity_reference 'datedistributionid' (which is basically the date of distribution plus other fields) and 'amapienid' (which is mainly the people who can attend to the distribution).  

I want to create a form allowing people to enroll to the distribution, i.e. choose a date among all possible and their role. 

In the past (a module I wrote for Drupal 6), I had a db_query joining the three tables, sorting by date, name of the people, etc. I processed the result of the query with a while ($record = db_fetch_object($result)) to build a tabular form with all dates and already enrolled people and so on.

What would be the "Drupal 8" way to do that?

I started with a   

$storage = \Drupal::entityManager()-&gt;getStorage('amap_distributions_inscriptions');  
$ids = \Drupal::entityQuery('amap_distributions_inscriptions')  
  -&gt;execute();  
$inscriptions = $storage-&gt;loadMultiple($ids);  
foreach ($inscriptions as $id=&gt;$inscription) {my code here}  


but I'm struggling with retrieving 'date distribution' and 'amapien', then sorting the results before starting to build the form rows...

Any guidance?

Trying to use @ssibal's advice, I assume I should write something like

$database = \Drupal::database();
$result = $database
  -&gt;select('amap_distributions_dates', ???)
  -&gt;fields('ddate', 'iid', 'pid', 'irole', 'nom', 'prenom')
  -&gt;condition('dnumber', 0, '&gt;')
  -&gt;execute()


My D6 query ('amap_distributions_dates', 'amap_distributions_inscriptions' and 'association_personnes' being the 3 tables):

$query = "
SELECT 
    amap_distributions_dates.ddate, 
    iid, 
    amap_distributions_inscriptions.pid, 
    irole,
    nom, 
    prenom
FROM 
    amap_distributions_dates 
    LEFT JOIN amap_distributions_inscriptions ON amap_distributions_inscriptions.ddate = amap_distributions_dates.ddate
    LEFT JOIN association_personnes ON association_personnes.pid = amap_distributions_inscriptions.pid 
WHERE
    dnumber &gt; 0
ORDER BY 
    amap_distributions_dates.ddate ASC, 
    iid ASC,
    irole ASC, 
    nom ASC, 
    prenom ASC
";
Finally, this worked:

$database = \Drupal::database();
$query = $database-&gt;select('amap_distributions_dates', 'amdd');
$query-&gt;leftJoin('amap_distributions_inscriptions', 'amdi', 'amdi.datedistributionid = amdd.id');
$query-&gt;leftJoin('association_personne', 'ap', 'ap.id = amdi.amapienid');
$query-&gt;fields('amdd', ['id', 'datedistribution', 'nombreproduits'])
    -&gt;fields('amdi', ['id', 'datedistributionid', 'amapienid', 'role'])
    -&gt;fields('ap', ['id', 'nom', 'prenom'])
    -&gt;condition('nombreproduits', 0, '&gt;')
    -&gt;orderBy('datedistribution', 'ASC')
    -&gt;orderBy('role', 'ASC')
    -&gt;orderBy('nom', 'ASC')
    -&gt;orderBy('prenom', 'ASC');
$results = $query-&gt;execute()-&gt;fetchAllAssoc('id');
foreach ($results as $row) { some code here }