3

I followed SPARQL 1.1 section 10.2 CONSTRUCT and have the following SPARQL query which returns all triples whose subject has type Homework and which also have an event date.

CONSTRUCT  { ?s ?p ?o } WHERE 
{
 GRAPH ?g { ?s ?p ?o } .
 { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   
    <http://lod.isi.edu/ontology/syllabus/Homework> 
 }.

 { ?s <http://lod.isi.edu/ontology/syllabus/hasEventDate> ?date}.
}

Now, I want to retrieve the results in ascending/descending order of the event date, ?date. I tried adding the order by modifier as shown below, but there are parse errors in the OpenRDF workbench.

CONSTRUCT  { ?s ?p ?o } WHERE 
{
 GRAPH ?g { ?s ?p ?o } .
 { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   
    <http://lod.isi.edu/ontology/syllabus/Homework> 
 }.

 { ?s <http://lod.isi.edu/ontology/syllabus/hasEventDate> ?date}.
 ORDER BY DESC(?date)

}
Encountered " "order" "ORDER "" at line 9, column 6. Was expecting one of:
"(" ... "{" ... "}" ... "[" ... <NIL> ... <ANON> ... "optional" ...
"graph" ... "minus" ... "filter" ... "true" ... "false" ... "bind" ...
"service" ... "values" ... <Q_IRI_REF> ... <PNAME_NS> ... <PNAME_LN> ...
<BLANK_NODE_LABEL> ... <VAR1> ... <VAR2> ... <INTEGER> ... <INTEGER_POSITIVE> ...
<INTEGER_NEGATIVE> ... <DECIMAL> ... <DECIMAL_POSITIVE> ...
<DECIMAL_NEGATIVE> ... <DOUBLE> ... <DOUBLE_POSITIVE> ...
<DOUBLE_NEGATIVE> ... <STRING_LITERAL1> ... <STRING_LITERAL2> ...
<STRING_LITERAL_LONG1> ... <STRING_LITERAL_LONG2> ...
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Margi
  • 455
  • 1
  • 9
  • 20
  • What was the exact syntax that you used, and what error did you get? – Jeen Broekstra Feb 26 '14 at 01:39
  • Also please show sample output from the query - both actual output and expected output. – Jeen Broekstra Feb 26 '14 at 01:50
  • 1
    The accepted answer explains how you can fix the syntax issue, but it's important to note that the result of a `construct` query is an RDF graph, which is a set (and thus an _unordered_ collection) of triples. The `order by` doesn't do anything for you here unless you use it in conjunction with `limit` (and possibly `offset`) to select some subset of all the possible results. – Joshua Taylor Feb 26 '14 at 15:06
  • 1
    @JoshuaTaylor While the SPARQL standard makes no guarantees on the order of triples in the completed RDF graph (simply because such a graph is by definition unordered), implementations will still need to process the solution sequence in ordered form. Thus, in query engines that report query results in an iterative fashion, triples will in practice be reported in the defined order. – Jeen Broekstra Feb 26 '14 at 19:22
  • 2
    @JeenBroekstra Yes, and that's a good point; this might make the results a little bit more palatable to certain applications. Nonetheless, Margi "[wants] to retrieve the results in ascending/descending order of the event date", and while this fixes the parse error, it's not a guaranteed way of "[retrieving] the results in ascending/descending order of the event date". I just don't want to mislead Margi. :) – Joshua Taylor Feb 26 '14 at 19:23

1 Answers1

3

The problem is that your ORDER BY clause is not in the correct place in the query. It should be after the closing bracket that closes the WHERE clause:

CONSTRUCT  { ?s ?p ?o } 
WHERE 
{
 GRAPH ?g { ?s ?p ?o } 
 ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://lod.isi.edu/ontology/syllabus/Homework> .
 ?s <http://lod.isi.edu/ontology/syllabus/hasEventDate> ?date .
}
ORDER BY ASC(?date)

Also note that several of the curly braces in your original query are, although not exactly wrong, superfluous.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • 3
    That will fix the syntax issue, but it's also important to note that the result of a `construct` query is a graph, i.e., an (unordered) _set_ of triples; `order by` doesn't really do anything here unless it's used in conjunction with `limit` (and possibly `offset`) to restrict the contents of the result. – Joshua Taylor Feb 26 '14 at 15:04
  • @JoshuaTaylor That is true. However, while the SPARQL standard makes no guarantees on the order of triples in the completed RDF graph (simply because such a graph is _by definition_ unordered), implementations will still need to process the solution sequence in ordered form. Thus, in query engines that report query results in an iterative fashion, triples will in practice be reported in the defined order. – Jeen Broekstra Feb 26 '14 at 19:20
  • This is not the case for Jena Fuseki, at least in the latest version. A `select` query returns results in an ordered manner, but a `construct` query returns results in an unordered manner. – Sébastien Rosset Feb 03 '21 at 08:56