Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andrecastro0o/SPARQL-cheatsheet
SPARQL cheatsheet
https://github.com/andrecastro0o/SPARQL-cheatsheet
rdf sparql
Last synced: 3 months ago
JSON representation
SPARQL cheatsheet
- Host: GitHub
- URL: https://github.com/andrecastro0o/SPARQL-cheatsheet
- Owner: andrecastro0o
- License: cc0-1.0
- Created: 2020-11-14T10:09:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-25T09:58:28.000Z (almost 3 years ago)
- Last Synced: 2024-06-23T01:00:27.040Z (6 months ago)
- Topics: rdf, sparql
- Homepage:
- Size: 26.4 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SPARQL-cheatsheet
All those SPARQL commands & tricks that you keep on forgetting ヽ༼ຈل͜ຈ༽ノ**Contribuitons are welcomed. Send you pull requests**
## Query Forms
### SELECT
...### [CONSTRUCT](https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#construct)
Returns a graph specified by a graph template.within the `CONSTRUCT` triples are created based on the variables established within the `WHERE` query
```SPARQL
PREFIX wd:
PREFIX wdt:
PREFIX mo:
prefix ex:
CONSTRUCT
{
?iri a mo:Movie ;
mo:hasWikidataLink ?s ;
mo:hasIMDBResource ?imdbRes ;
mo:title ?title ;
}
WHERE {
SERVICE
{
?s wdt:P31 wd:Q24869 ; #instance_of feature film
wdt:P345 ?imdbId ;
wdt:P1476 ?title .
BIND(IRI(CONCAT("https://www.imdb.com/title/", ?imdbId )) AS ?imdbRes) .
BIND( IRI(CONCAT("http://semantics.id/ns/example#", ?imdbId )) AS ?iri) .
}
}
LIMIT 2```
Resulting in new triples
```ttl
@prefix ex: .
@prefix mo: .
@prefix wd: .
@prefix wdt: .ex:tt0461770 a mo:Movie ;
mo:hasIMDBResource ;
mo:hasWikidataLink wd:Q490464 ;
mo:title "Enchanted"@en .ex:tt0070215 a mo:Movie ;
mo:hasIMDBResource ;
mo:hasWikidataLink wd:Q964009 ;
mo:title "Il mio nome è Nessuno"@it .
```
### ASK### DESCRIBE
## String Operations
* `UCASE()` convert to upper-case
* `LCASE()` convert to lower-case
* `STRLEN()` lenght of a string
* `SUBSTR()` substring of first arg, second arg: start position; third arg: character lenght;
* `STRSTARTS()`string in first arg starts with string in second arg
* `STRENDS()` string in first arg ends with string in second arg
* `CONTAINS()` string in second arg is within first arg. Example: `CONTAINS("SPARQL", "QL")` is True. `FILTER(CONTAINS(str(?term), "iao") || CONTAINS(str(?term), "bfo") || CONTAINS(str(?term), "obo")) `## Logic operators
* `sameTerm` & `!sameTerm` (more efficient and `=`)
* `=`, `!=`, `>`, `>=`, `<`, `<=`
* AND: `&&` OR: `||`
* `IN` and `NOT IN`: `?foo IN ("bar", "anotherbar")`
* [isIRI](https://www.w3.org/TR/rdf-sparql-query/#func-isIRI) `isIRI`
* [isBlank](https://www.w3.org/TR/rdf-sparql-query/#func-isBlank): only bank nodes `FILTER (isBlank(?term))` filter out `FILTER (!isBlank(?term))`
* [isLiteral](https://www.w3.org/TR/rdf-sparql-query/#func-isLiteral)## Functions
* [lang](https://www.w3.org/TR/rdf-sparql-query/#func-lang) Returns the language tag.
* `FILTER(lang(?label) = 'es')`
* *any* `@en` variant (will match all of these: `@en`, `@en-US`, `@en-GB`) `FILTER(langMatches(lang(?label), 'en'))`
* [datatype](https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#func-datatype) returns the datatype of a literal
* `BIND( datatype(?shoeSize) AS ?datatype)`## Aggregation Functions
* `COUNT`
* `SUM`
* `AVG`
* `MIN`, `MAX`
* `SAMPLE`
* `GROUP_CONCAT`----
# Tips to speedup SPARQL Queries:
* reduce search space
* avoid `OPTIONAL` clauses (when possible), or have them follown non-optional clauses
* avoid `FILTER` or try having them earlier - so search space is reduced:
* `FILTER(sameTerm(?term1, ?term2))` is faster that `FILTER(?terms = ?term)`
* idem for `!sameTerm` being faster that `!=`
* regexs are expensive
* avoid `DISTINCT`
* avoid `SORT BY` in large amount of results
* reduce printout named variables - allows for more optimization flexibility from the SPARQL processor# SPARQL Resources
* “SPARQL Functions Reference — GraphDB Free 9.10.0 Documentation.” https://graphdb.ontotext.com/documentation/free/sparql-functions-reference.html.
* DuCharme, Bob. Learning SPARQL: Querying and Updating with SPARQL 1.1. Second edition. Sebastopol, CA: O’Reilly Media, 2013.
* “SPARQL 1.1 Query Language.” https://www.w3.org/TR/sparql11-query/.
* “Bob DuCharme Blog.” http://www.bobdc.com/blog/.
* “Module: SPARQL — Web Portal for RDF.Rb.” http://rdf.greggkellogg.net/yard/SPARQL.html.
* “Wikidata:Request a Query - Wikidata.” https://www.wikidata.org/wiki/Wikidata:Request_a_query.