Search code examples
sparql

What problem does the dot solve in SPARQL queries?


In every tutorial or reference I look at . seems to be used at the end of every line, but it is never explained why it is there. And with the more complex queries it seems the dot is sometimes inside the { } and sometimes comes immediately after. And then sometimes isn't used at all. This is driving me crazy, as without knowing its purpose I feel I'm going be troubleshooting queries by trial and error: throw a dot in and repeat, try deleting a dot and repeat.

My main question is what breaks, or what kind of queries can't be written, if you don't have . as part of the syntax?

Explaining in terms of "it is just like the something in SQL" or "does the same as the something in a regex" might also be helpful.


Example of how optional the . is. This (chess players born in first 3 months of 2001) can be written with 6 dots:

SELECT ?person ?personLabel ?genderLabel ?dob
WHERE {
  ?person wdt:P31 wd:Q5;
          wdt:P106 wd:Q10873124;
          wdt:P569 ?dob.
  OPTIONAL{ ?person wdt:P21 ?gender. }.
  FILTER("2001-01-02"^^xsd:dateTime <= ?dob && ?dob < "2001-03-31"^^xsd:dateTime)
  .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en".} .
}

But can also be written with no dots at all:

SELECT ?person ?personLabel ?genderLabel ?dob
WHERE {
  ?person wdt:P31 wd:Q5;
          wdt:P106 wd:Q10873124;
          wdt:P569 ?dob
  OPTIONAL{ ?person wdt:P21 ?gender }
  FILTER("2001-01-02"^^xsd:dateTime <= ?dob && ?dob < "2001-03-31"^^xsd:dateTime)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en"} 
}

Solution

  • SPARQL is a language for querying RDF triples. Each pattern matches a triple, and the syntax for these triple patterns is the same as in the Turtle or N3 syntax. You need the . to delimit multiple triples in a list, although the final . is optional. If you have multiple triples with the same subject or subject-predicate, you can combine them using , and ;. Whitespace is not significant in SPARQL.