Example of Craft 3.0 plugin - querying an existing table
A great place to start with this is over on https://pluginfactory.io/.

Select "Craft CMS Plugin Version 3.x" and check any plugin components you'd like to include, and it will build out the plugin scaffolding for you with ample comments and some examples.

For accessing database content, Craft heavily relies on the framework it uses, Yii 2.  The "Working with Databases" docs are a good place to start: https://www.yiiframework.com/doc/guide/2.0/en/start-databases#working-with-databases

It will go over how to retrieve data using Active Record (Craft's AR classes) and Query Builder for direct database access.

You can do that data access from your plugin's Variable class, but a more common pattern is for the Variable class to have a wrapper method that calls a Service class method that returns the value to the Variable, then back to the template.

You can see Craft's Service classes here: https://github.com/craftcms/cms/tree/develop/src/services

And in case it's not obvious yet, the Craft source code itself is a great place to look for examples of how to do things in your plugin.