backend code for database queries