Query only Tags wich are not connected to a revision I wrote a query like this:
 $query = (new Query())
        -&gt;select('COUNT(elements.id) as count, tags.*')
        -&gt;from('relations')
        -&gt;join('INNER JOIN', 'tags', '{{relations}}.targetId={{tags}}.id')
        -&gt;join('LEFT JOIN', 'content', '{{content}}.elementId={{tags}}.id')
        -&gt;join('LEFT JOIN', 'elements', '{{elements}}.id={{tags}}.id')
        -&gt;where([
            'elements.enabled' =&gt; 1,
            'elements.revisionId' =&gt; NULL
        ])
        -&gt;groupBy('{{relations}}.targetId')
        -&gt;orderBy('count DESC');

So my intention there is to fetch all tags wich have content and do have revisionId IS NULL.
But it never works. I get just all the relations including these one related to elements with a revisionId not null.
Can someone help me in this issue.
I am completely new to craft and use to the symfony query builder wich is kind of different.
I found the solution and posted it here in another issue related to this problem.
Here is my solution (just the code):
$query = (new Query())
    -&gt;select([
        '{{relations}}.targetId as id',
        'COUNT({{relations.targetId}}) as count',
        '{{content}}.title as title'
    ])
    -&gt;from('relations')
    -&gt;leftJoin('tags', '{{relations}}.targetId={{tags}}.id')
    -&gt;leftJoin('content', '{{content}}.elementId={{tags}}.id')
    -&gt;leftJoin('elements', '{{elements}}.id={{tags}}.id')
    -&gt;leftJoin('elements source', '{{source}}.id={{relations}}.sourceId')
    -&gt;where([
        '{{elements}}.enabled' =&gt; 1,
        '{{source}}.enabled' =&gt; 1,
        '{{source}}.revisionId' =&gt; NULL,
        '{{source}}.draftId' =&gt; NULL,
    ])
    -&gt;groupBy('id, title')
    -&gt;orderBy( 'count DESC, title ASC')
;

$query-&gt;limit($limit);

if (is_numeric($limit) &amp;&amp; !is_null($limit)) {
    $query-&gt;limit($limit);
}

$results = $query-&gt;all();

foreach ($results as $key =&gt; $value) {
    $id = $value['id'];
    $results[$key] = Tag::find()-&gt;id($id)-&gt;one();
}

return $results;