query db correctly