refactored Database Qeury