You can create a listener for when features are selected as below.
this.own(on(this.map.infoWindow, 'selection-change', lang.hitch(this, this._onSelectionChange)));
Then in your "_onSelectionChange" function you can check the details of the feature and do your query. I have something similar set up to query related records out of an a-spatial database, then add them to the popup under the feature details.
_onSelectionChange: function (selectedFeature) {
console.log(selectedFeature);
//use the selected feature to run your query.
//nl should return the main section of your popup.
var nl = query('.mainSection');
if(nl.length > 0){
//Add your table details in to this div.
var div = domConstruct.toDom(drawing_data);
//Then append it to the popup
nl[0].appendChild(div);
}
}
In the above you'll need the following.
query = dojo/query
domConstruct = dojo/dom-construct
The above may not be perfect as its from my web appbuilder application, but it should help you along the way.