Add Query to Tag Logic (Legacy)
If you want to query data that match conditions or values from a Related List, you need to add the query to the Tag Logic of the related list's child element. Data from query created in a Tag Logic of an element can be used only for that specified element.
In Document Template , select the desired element
In the right pane, select Tag -> Attributes -> Logic to open the tag logic

In the left pane, type "Query" to the Filter box
Drag and drop the query tag to the workspace

Give a name for the query's var

In select attribute, type the SOQL syntax or select "..." button to open the Query Editor
Learn more how to: