tidy up query implementation

use a common query interface instead of a master select statement