Search code examples
sparqlrdfsemantic-webdbpedia

Querying DBPedia using SPARQL to find subjects with same objects


I am trying to find all those resources from dbpedia for eg rdf:type person who have same object eg date of birth.? I thought of doing it with subquery but its definitely not the solution. Can anyone provide some useful pointer?


Solution

  • From what you describe I think you mean:

    prefix dbp: <http://dbpedia.org/property/>
    prefix foaf: <http://xmlns.com/foaf/0.1/>
    
    select ?s1 ?s2 ?dob
    where {
        ?s1 a foaf:Person ; dbp:birthDate ?dob . # Find a person, get their dob
        ?s2 a foaf:Person ; dbp:birthDate ?dob . # Find a person with the same dob
    }
    

    Adjust type and predicate to suit.

    This will include some redundancy: you will find answers where the subjects are the same ('Napoleon' 'Napoleon') and get answers twice ('Daniel Dennett' 'Neil Kinnock', 'Neil Kinnock' 'Daniel Dennett'). You can remove that with a filter:

    filter (?s1 < ?s2)
    

    which just ensures that one comes before the other (however the query engine wants to do that).

    prefix dbp: <http://dbpedia.org/property/>
    prefix foaf: <http://xmlns.com/foaf/0.1/>
    
    select ?s1 ?s2 ?dob
    where {
        ?s1 a foaf:Person ; dbp:birthDate ?dob .
        ?s2 a foaf:Person ; dbp:birthDate ?dob .
        filter (?s1 < ?s2)
    }
    

    See the result