Is it possible to run a badge query against data in another db or a csv? We’d like to give a badge to users who match certain criteria in our (ie non-Discourse) accounts database, but not just retroactively.
You will have to write a plugin for that.
Another solution would be to do that via the api.
The easiest way to grant a badge based on activity that occurs outside of Discourse would be to create a custom badge on Discourse through the UI, and then grant the badge through the Discourse API. See How to grant a custom badge through the API for details.