How to perform a webform_submission query with a conditional based on a custom field in the submission data I'm attempting to query all webforms with a specific id, and sorted by a boolean field that was added to a webform by me in the webform configuration manager. 
Below is my attempt

    $submission_ids = \Drupal::entityQuery('webform_submission')
        -&gt;condition('webform_id', $webform_id)
        -&gt;exists('paid')
        -&gt;sort('paid', 'desc')
        -&gt;sort('created', 'desc')
        -&gt;pager(50)
        -&gt;execute();


However when I perform this query I get


  Drupal\Core\Entity\Query\QueryException: 'paid' not found in
  Drupal\Core\Entity\Query\Sql\Tables->ensureEntityTable() (line 316 of
  core/lib/Drupal/Core/Entity/Query/Sql/Tables.php).


If I omit the ->sort('paid','desc'), but keep the ->sort('created', 'desc') the query works fine. I'm assuming this is because created is one of the base fields defined in the webform entity.

I also tried using 'field.paid', and 'data.paid' as a guess but I ended up with


  Call to a member function getColumns() on boolean in /var/www/aaep.org/http/web/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
  on line 228


Is there a way to sort webform submissions by user defined fields in an entity query?
You first need to select the Submission IDs (SIDs) from the table webform_submission_data:

$select = \Drupal::service('database')
  -&gt;select('webform_submission_data', 'wsd')
  -&gt;fields('wsd', array('sid'))
  -&gt;orderBy('wsd.sid', 'DESC')
  -&gt;condition('wsd.webform_id', $webform_id, '=')
  -&gt;condition('wsd.name', 'paid', '=')
  -&gt;condition('wsd.value', 1, '=')// Not sure how your BOOL value is saved
  -&gt;execute();

// Get all the results.
$results = $select-&gt;fetchAll(\PDO::FETCH_COLUMN);


Then you need to use entity_load_multiple() or similar to load the entities:

if (count($results) &gt;= 1) {
  $submissions = \Drupal::entityTypeManager()
    -&gt;getStorage('webform_submission')
    -&gt;loadMultiple($results);

  foreach ($submissions as $submission) {
    // Do something
  }
}


Drupal.org thread Reference for basis of my answer.