rdfsparqlrestrictionsrdfscardinality

Cardinality restrictions on SPARQL


My question is about SPARQL query language of RDF triples, suppose we have a family ontology written in RDF/XML format.

Now, I want to query all parents, for example, with at least two children (cardinality on hasChild relation) with SPARQL.

My question is, is it possible to write this query in SPARQL language, however I know this is possible to write this query in DL query language (Description Logic)

In more general form, Is it possible to apply some cardinality restrictions in SPARQL language?


Solution

  • Now, I want to query all parents, for example, with at least two children (cardinality on hasChild relation) with SPARQL.

    You just select a parent and child in each row, then group by the parent, and then only take those that have at least two values for child:

    select ?parent where {
      ?parent :hasChild ?child
    }
    group by ?parent
    having (count(distinct ?child) >= 2)
    

    Beware though; In OWL you can have an individual that must have at least two children, but that this query wouldn't return. E.g., if you have

    TwoChildParent subClassof (hasChild min 2)
    Joe a TwoChildParent
    

    but don't have any

    Joe hasChild ?x
    

    triples, this query won't return Joe, even though Joe has at least two children.