Querying across relationships in a template I have two Sections on my site: Country and Alert. The Alert section has a relation with Country via an "Entries" field. An Alert represents something that is happening in a particular country for a period of time so it will have a post date and an expiry date. 

In my template, I want to show a list of the 5 Countries that have currently active Alerts (ideally ordered by the country with the latest Alert).

I'm not sure how query across relationships in Craft (outside of using the relatedTo query, but that only allows you to related to one entry, not a number of entries)? It seems all relationships are handled in a separate relation table in the DB.

How can I achieve an query such as this within a Craft template?
OK, here is one solution I've come up with, which seems to work and (I think) avoids 1+ queries:

{% set groups = craft.entries({
    section: 'alert',
    with: [
        'country',
    ]
}).orderBy('postDate desc').status('live').all() | group(alert =&gt; alert.country[0]) %}
&lt;ul&gt;
    {% for country, alerts in groups | slice(0, 5) %}
        &lt;li&gt;
            {{ country }} &lt;!-- Cannot use country Entry --&gt;
            &lt;ul&gt;
                {% for alert in alerts %}
                    &lt;li&gt;{{ alert.title }}&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;


I would prefer being able to handle this entirely in queries though