Search code examples
sparqlsemantics

How to get all semantic data that is common between three classes using SPARQL?


I have a scenario where the user can select multiple classes and i need to build dynamic SPARQL query to fetch all the data that satisfies all the relationships thats exist (if any )between all selected classes. How do i achieve this?

Suppose the user selects three classes- Population, Drugname,SideEffects. Now he tries to find all the common data that exists between these three classes. How do i build a SPARQL query to achieve this? I need a sample SPARQL query that joins these three classes.


Solution

  • If you want to retrieve properties for which some resources have the same value (and so you probably want to retrieve that value, too), you can use a query like this:

    select ?p ?o where {
      dbpedia:Bob_Dylan ?p ?o .
      dbpedia:Tom_Waits ?p ?o .
      dbpedia:The_Byrds ?p ?o .
    }
    

    SPARQL results from DBpedia

    p                                                o
    --------------------------------------------------------------------------------------------------------------------------
    http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://www.w3.org/2002/07/owl#Thing
    http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://dbpedia.org/ontology/Agent
    http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://schema.org/MusicGroup
    http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://dbpedia.org/class/yago/YagoLegalActor
    http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://dbpedia.org/class/yago/YagoLegalActorGeo
    http://purl.org/dc/terms/subject                 http://dbpedia.org/resource/Category:Rock_and_Roll_Hall_of_Fame_inductees
    http://dbpedia.org/ontology/recordLabel          http://dbpedia.org/resource/Asylum_Records
    http://dbpedia.org/ontology/genre                http://dbpedia.org/resource/Rock_music
    http://dbpedia.org/property/genre                http://dbpedia.org/resource/Rock_music
    http://dbpedia.org/property/label                http://dbpedia.org/resource/Asylum_Records
    http://dbpedia.org/property/wordnet_type         http://www.w3.org/2006/03/wn/wn20/instances/synset-musician-noun-1
    

    You could also use a query like this to find the subjects that are related to all three of these individuals by some particular property (but on DBpedia it doesn't have any answers):

    select ?s ?p where {
      ?s ?p dbpedia:Bob_Dylan, dbpedia:Tom_Waits, dbpedia:The_Byrds .
    }