Query based on foreign key fields