Selenium itself can’t do that as it is only for interacting with the HTML page.
How you do this will depend on your language, e.g. Java, Ruby, etc. and/or any tooling you are using such as editor.
Word of caution – we started doing this at one company I worked at previous but quickly abandoned it due to one reason
The asynchronous nature of the web made intermittent errors (the worst kind, e.g. 1% of the time, worst as impossible to reliably reproduce).
This will often lead you to not check the database in the UI tests but instead make sure that of the three levels of automation of
that verify database connections and calls. After all your browsser itself doesn’t actually call the database, it calls the web server when then handles making that call through application logic on the web/application server(s)