Should not need to do this by database type. If it's getting that complex - then let's solve the problem by creating a relationship between the contact table and the content -- or -- provide the contact data needed using a plugin. This query is already over the top complicating. There should be no need for specific SQL for a database type.