Build select query