You can use limits with subqueries
You can use restrictions in subqueries. Here is an example:
select ?x ?y where { values ?x { 1 2 3 4 } { select ?y where { values ?y { 5 6 7 8 } } limit 2 } } limit 5
--------- | x | y | ========= | 1 | 5 | | 1 | 6 | | 2 | 5 | | 2 | 6 | | 3 | 5 | ---------
As you can see, you get two values ββfrom the subquery (5 and 6), and they are combined with the bindings from the external query, from which we get only five rows (due to the restriction).
Subqueries are first evaluated first
However, keep in mind that subqueries are evaluated from the most internal to the most external. This means that in your request
select ?person_id ?place_of_birth ?other_person_id where { ?person_id fb:type.object.type fb:people.person . ?person_id fb:people.person.place_of_birth ?place_of_birth_id . ?place_of_birth_id fb:type.object.name ?place_of_birth . { select ?other_person_id where { ?place_of_birth_id fb:location.location.people_born_here ?other_person_id . } LIMIT 1 } FILTER (langMatches(lang(?place_of_birth),"en")) } LIMIT 3
you will find one match for
?place_of_birth_id fb:location.location.people_born_here ?other_person_id .
and transfers ? other_person_id bindings to an external request. Does the rest of the external query not use ? Other_person_id , so in fact this has no effect on the results.
What to do instead
If you only need one person
The application needs a query that returns the birthplace of each person and another person born in the same place.
Conceptually, you could look at this, how to recruit a person, find a place of birth and select another person from people born in this place. You can also write this query:
select ?person_id ?place_of_birth (sample(?other_person_idx) as ?other_person_id) where { ?person_id fb:type.object.type fb:people.person . ?person_id fb:people.person.place_of_birth ?place_of_birth_id . ?place_of_birth_id fb:type.object.name ?place_of_birth . FILTER (langMatches(lang(?place_of_birth),"en")) ?place_of_birth_id fb:location.location.people_born_here ?other_person_idx . filter ( ?other_person_idx != ?person_id ) } group by ?person_id ?place_of_birth
If you need more than one
This is a much more complex problem if more than one βother resultβ is required for each result. This is a problem in Nested queries in sparql with restrictions . Q How to limit the size of a SPARQL solution group? which can be used for this.