server logic for query database