Yes, you can do a direct sql query through the mendix runtime java api: https://apidocs.mendix.com/7/runtime/com/mendix/datastorage/DataStorage.html#executeWithConnection-java.util.function.Function-
Some example java code can be found here: https://github.com/ako/QueryApiBlogPost/blob/master/readme.md
In the Mendix cloud you cannot do a direct query on your mendix database.
In that case your can query thought the runtime, by exposing a web/rest service or exposing a table with OData.
Not exactly what you are looking for but close:
This way you do not affect the prod environment in any way, you will not change data by mistake, and you will not add extra load on the db by executing complicated or long lasting queries.
-Andrej