query to extract database