It is not possible to query a database from within a template. Are you producing content via OL Connect Workflow? In that case you could query the database as part of your flow and pass data in JSON format.
It’s possible from workflow ,
so my tested case we have two tables one master and other is details .
we connected to details table from data mapper and did logic and get other data from master table while processing the workflow .
I hope this help you .