Further to @david.wylie’s pointers, the Anvil docs have some code examples for connecting to common external DBs such as MySQL or Postgres:
https://anvil.works/docs/data-tables/external-database
If you want to execute SQL queries against Anvil’s Data Tables, that’s available on the Dedicated Plan (read more in the docs here).