SQL queries in replacement for custom views in DB