This is something for the Data Explorer plugin, it allows you to run custom SQL queries against the Discourse database.
If you’re somewhat versed in SQL, using Data Explorer is pretty easy since it has included help to show you the database scheme; otherwise, I’m sure you’ll get some help here if you describe what you want, exactly