| 1 | <h2>Introduction</h2> |
| 2 | |
| 3 | <p>The queries shown here can be found in the SPARQL specifications: |
| 4 | <a href="https://www.w3.org/TR/sparql11-query/">https://www.w3.org/TR/sparql11-query/</a></p> |
| 5 | |
| 6 | <h2>query 2.1.6 Examples of Query Syntax</h2> |
| 7 | |
| 8 | <pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 9 | SELECT ?title |
| 10 | WHERE { <http://example.org/book/book1> dc:title ?title } |
| 11 | </code></pre> |
| 12 | |
| 13 | <h2>query 2.1.6-q1 Examples of Query Syntax</h2> |
| 14 | |
| 15 | <pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 16 | PREFIX : <http://example.org/book/> |
| 17 | SELECT $title |
| 18 | WHERE { :book1 dc:title $title } |
| 19 | </code></pre> |
| 20 | |
| 21 | <h2>query 2.1.6-q2 Examples of Query Syntax</h2> |
| 22 | |
| 23 | <pre><code>BASE <http://example.org/book/> |
| 24 | PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 25 | SELECT $title |
| 26 | WHERE { <book1> dc:title ?title } |
| 27 | </code></pre> |
| 28 | |
| 29 | <h2>query 2.5.3 Example of Basic Graph Pattern Matching</h2> |
| 30 | |
| 31 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 32 | SELECT ?mbox |
| 33 | WHERE |
| 34 | { ?x foaf:name "Johnny Lee Outlaw" . |
| 35 | ?x foaf:mbox ?mbox } |
| 36 | </code></pre> |
| 37 | |
| 38 | <h2>query 3.1.1 Matching Integers</h2> |
| 39 | |
| 40 | <pre><code>SELECT ?v WHERE { ?v ?p 42 }</code></pre> |
| 41 | |
| 42 | <h2>query 3.1.2 Matching Arbitrary Datatypes</h2> |
| 43 | |
| 44 | <pre><code>SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }</code></pre> |
| 45 | |
| 46 | <h2>query 3.1.3-q1 Matching Language Tags</h2> |
| 47 | |
| 48 | <pre><code>SELECT ?x WHERE { ?x ?p "cat"@en }</code></pre> |
| 49 | |
| 50 | <h2>query 3.2 Value Constraints</h2> |
| 51 | |
| 52 | <pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 53 | PREFIX ns: <http://example.org/ns#> |
| 54 | SELECT ?title ?price |
| 55 | WHERE { ?x ns:price ?price . |
| 56 | FILTER (?price < 30) . |
| 57 | ?x dc:title ?title . } |
| 58 | </code></pre> |
| 59 | |
| 60 | <h2>query 5.5 Nested Optional Graph Patterns</h2> |
| 61 | |
| 62 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 63 | PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> |
| 64 | SELECT ?foafName ?mbox ?gname ?fname |
| 65 | WHERE |
| 66 | { ?x foaf:name ?foafName . |
| 67 | OPTIONAL { ?x foaf:mbox ?mbox } . |
| 68 | OPTIONAL { ?x vcard:N ?vc . |
| 69 | ?vc vcard:Given ?gname . |
| 70 | OPTIONAL { ?vc vcard:Family ?fname } |
| 71 | } |
| 72 | } |
| 73 | </code></pre> |
| 74 | |
| 75 | <h2>query 6.1-q2 Joining Patterns with UNION</h2> |
| 76 | |
| 77 | <pre><code>PREFIX dc10: <http://purl.org/dc/elements/1.1/> |
| 78 | PREFIX dc11: <http://purl.org/dc/elements/1.0/> |
| 79 | |
| 80 | SELECT ?title ?author |
| 81 | WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author } |
| 82 | UNION |
| 83 | { ?book dc11:title ?title . ?book dc11:creator ?author } |
| 84 | } |
| 85 | |
| 86 | </code></pre> |
| 87 | |
| 88 | <h2>query 8.3 Restricting by Bound Variables</h2> |
| 89 | |
| 90 | <pre><code>PREFIX data: <http://example.org/foaf/> |
| 91 | PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 92 | PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 93 | |
| 94 | SELECT ?mbox ?nick ?ppd |
| 95 | WHERE |
| 96 | { |
| 97 | GRAPH data:aliceFoaf |
| 98 | { |
| 99 | ?alice foaf:mbox <mailto:[email protected]> ; |
| 100 | foaf:knows ?whom . |
| 101 | ?whom foaf:mbox ?mbox ; |
| 102 | rdfs:seeAlso ?ppd . |
| 103 | ?ppd a foaf:PersonalProfileDocument . |
| 104 | } . |
| 105 | GRAPH ?ppd |
| 106 | { |
| 107 | ?w foaf:mbox ?mbox ; |
| 108 | foaf:nick ?nick |
| 109 | } |
| 110 | } |
| 111 | </code></pre> |
| 112 | |
| 113 | <h2>query 9.3 Combining FROM and FROM NAMED</h2> |
| 114 | |
| 115 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 116 | PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 117 | |
| 118 | SELECT ?who ?g ?mbox |
| 119 | FROM <http://example.org/dft.ttl> |
| 120 | FROM NAMED <http://example.org/alice> |
| 121 | FROM NAMED <http://example.org/bob> |
| 122 | WHERE |
| 123 | { |
| 124 | ?g dc:publisher ?who . |
| 125 | GRAPH ?g { ?x foaf:mbox ?mbox } |
| 126 | } |
| 127 | </code></pre> |
| 128 | |
| 129 | <h2>query 10.1.2 DISTINCT</h2> |
| 130 | |
| 131 | <pre><code> |
| 132 | PREFIX foaf: <http://xmlns.com/foaf/0.1//> |
| 133 | SELECT DISTINCT ?name WHERE { ?x foaf:name ?name } |
| 134 | </code></pre> |
| 135 | |
| 136 | <h2>query 10.1.3-q2 ORDER BY</h2> |
| 137 | |
| 138 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 139 | |
| 140 | SELECT ?name |
| 141 | WHERE { ?x foaf:name ?name ; :empId ?emp } |
| 142 | ORDER BY ?name DESC(?emp) |
| 143 | </code></pre> |
| 144 | |
| 145 | <h2>query 10.1.5 OFFSET</h2> |
| 146 | |
| 147 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 148 | |
| 149 | SELECT ?name |
| 150 | WHERE { ?x foaf:name ?name } |
| 151 | ORDER BY ?name |
| 152 | LIMIT 5 |
| 153 | OFFSET 10 |
| 154 | </code></pre> |
| 155 | |
| 156 | <h2>query 10.3.1 Templates with Blank Nodes</h2> |
| 157 | |
| 158 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 159 | PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> |
| 160 | |
| 161 | CONSTRUCT { ?x vcard:N _:v . |
| 162 | _:v vcard:givenName ?gname . |
| 163 | _:v vcard:familyName ?fname } |
| 164 | WHERE |
| 165 | { |
| 166 | { ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } . |
| 167 | { ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } . |
| 168 | } |
| 169 | </code></pre> |
| 170 | |
| 171 | <h2>query 10.3.3 Solution Modifiers and CONSTRUCT</h2> |
| 172 | |
| 173 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 174 | PREFIX site: <http://example.org/stats#> |
| 175 | |
| 176 | CONSTRUCT { [] foaf:name ?name } |
| 177 | WHERE |
| 178 | { [] foaf:name ?name ; |
| 179 | site:hits ?hits . |
| 180 | } |
| 181 | ORDER BY desc(?hits) |
| 182 | LIMIT 2</code></pre> |
| 183 | |
| 184 | <h2>query 10.4.3 Descriptions of Resources</h2> |
| 185 | |
| 186 | <pre><code>PREFIX ent: <http://org.example.com/employees#> |
| 187 | DESCRIBE ?x WHERE { ?x ent:employeeId "1234" } |
| 188 | </code></pre> |
| 189 | |
| 190 | <h2>query 10.5-q1 Asking "yes or no" questions</h2> |
| 191 | |
| 192 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 193 | ASK { ?x foaf:name "Alice" ; |
| 194 | foaf:mbox <mailto:[email protected]> } |
| 195 | </code></pre> |
| 196 | |
| 197 | <h2>query 11.4.1 bound</h2> |
| 198 | |
| 199 | <pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 200 | PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 201 | PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 202 | SELECT ?name |
| 203 | WHERE { ?x foaf:givenName ?givenName . |
| 204 | OPTIONAL { ?x dc:date ?date } . |
| 205 | FILTER ( bound(?date) ) }</code></pre> |
| 206 | |
| 207 | <h2>query 11.4.3 isBlank</h2> |
| 208 | |
| 209 | <pre><code> |
| 210 | PREFIX a: <http://www.w3.org/2000/10/annotation-ns#> |
| 211 | PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 212 | PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 213 | |
| 214 | SELECT ?given ?family |
| 215 | WHERE { ?annot a:annotates <http://www.w3.org/TR/rdf-sparql-query/> . |
| 216 | ?annot dc:creator ?c . |
| 217 | OPTIONAL { ?c foaf:given ?given ; foaf:family ?family } . |
| 218 | FILTER isBlank(?c) |
| 219 | }</code></pre> |
| 220 | |
| 221 | <h2>query 11.4.5 str</h2> |
| 222 | |
| 223 | <pre><code> |
| 224 | PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 225 | SELECT ?name ?mbox |
| 226 | WHERE { ?x foaf:name ?name ; |
| 227 | foaf:mbox ?mbox . |
| 228 | FILTER regex(str(?mbox), "@work.example") } |
| 229 | </code></pre> |
| 230 | |
| 231 | <h2>query 11.4.7 datatype</h2> |
| 232 | |
| 233 | <pre><code> |
| 234 | PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 235 | PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 236 | PREFIX eg: <http://biometrics.example/ns#> |
| 237 | SELECT ?name ?shoeSize |
| 238 | WHERE { ?x foaf:name ?name ; eg:shoeSize ?shoeSize . |
| 239 | FILTER ( datatype(?shoeSize) = xsd:integer ) } |
| 240 | </code></pre> |
| 241 | |
| 242 | <h2>query 11.4.10-q1 RDFterm-equal</h2> |
| 243 | |
| 244 | <pre><code> |
| 245 | PREFIX a: <http://www.w3.org/2000/10/annotation-ns#> |
| 246 | PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 247 | PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 248 | |
| 249 | SELECT ?annotates |
| 250 | WHERE { ?annot a:annotates ?annotates . |
| 251 | ?annot dc:date ?date . |
| 252 | FILTER ( ?date = xsd:dateTime("2004-01-01T00:00:00Z") || ?date = xsd:dateTime("2005-01-01T00:00:00Z") ) } |
| 253 | </code></pre> |
| 254 | |
| 255 | <h2>query 11.6-q1 Extensible Value Testing</h2> |
| 256 | |
| 257 | <pre><code> |
| 258 | PREFIX aGeo: <http://example.org/geo#> |
| 259 | |
| 260 | SELECT ?neighbor |
| 261 | WHERE { ?a aGeo:placeName "Grenoble" . |
| 262 | ?a aGeo:location ?axLoc . |
| 263 | ?a aGeo:location ?ayLoc . |
| 264 | |
| 265 | ?b aGeo:placeName ?neighbor . |
| 266 | ?b aGeo:location ?bxLoc . |
| 267 | ?b aGeo:location ?byLoc . |
| 268 | |
| 269 | FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) < 10 ) . |
| 270 | } |
| 271 | </code></pre> |
| 272 | |
| 273 | <p>The final example query is not based on the SPARQL 1.1 queries.</p> |
| 274 | |
| 275 | <h2>Full Example query</h2> |
| 276 | |
| 277 | <pre><code> |
| 278 | base <http://example.org/geo#> |
| 279 | prefix geo: <http://www.opengis.net/ont/geosparql#> |
| 280 | prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 281 | select ?shape ?shapeColor ?shapeHeight ?shapeName (sample(?shapeLabel) as ?shapeLabel) { |
| 282 | { |
| 283 | select * { |
| 284 | values (?placeName ?streetName) { |
| 285 | "Grenoble" "Paul Mistral" |
| 286 | } |
| 287 | ?place geo:NamePlace ?placeName. |
| 288 | ?pand geo:hasGeometry/geo:asWKT ?shape; |
| 289 | } |
| 290 | |
| 291 | } |
| 292 | ?pand geo:measuredHeight ?shapeHeight. |
| 293 | # Only retrieve buildings larger then 10 meters. |
| 294 | FILTER ( ?shapeHeight < 10 ) . |
| 295 | BIND(IF(!bound(?EindGeldigheid5), "#22b14c", "#ed1c24" ) AS?tColor) |
| 296 | # tekst label |
| 297 | bind(concat(str(?streetName),' ',str(?houseNumber),', ',str(?PlaceName)) as ?shapeName) |
| 298 | bind("""Multi-line |
| 299 | String Element |
| 300 | """ as ?shapeLabel) |
| 301 | } |
| 302 | group by ?shape ?shapeColor ?shapeHeight ?shapeName |
| 303 | limit 10 |
| 304 | </code></pre> |