SPARQL Examples: Difference between revisions
From info216
(→Limit) |
|||
Line 11: | Line 11: | ||
===List distinct properties only (with limit)=== | ===List distinct properties only (with limit)=== | ||
<syntaxhighlight> | <syntaxhighlight > | ||
SELECT DISTINCT ?p WHERE { | SELECT DISTINCT ?p WHERE { | ||
?s ?p ?o . | ?s ?p ?o . | ||
} | } | ||
LIMIT 10 | LIMIT 10 | ||
Line 19: | Line 19: | ||
===Limit with offset=== | ===Limit with offset=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
SELECT DISTINCT ?p WHERE { | SELECT DISTINCT ?p WHERE { | ||
?s ?p ?o . | ?s ?p ?o . | ||
} | } | ||
LIMIT 10 OFFSET 9 | LIMIT 10 OFFSET 9 | ||
Line 27: | Line 27: | ||
===List rdf:types=== | ===List rdf:types=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
SELECT DISTINCT ?t WHERE { | SELECT DISTINCT ?t WHERE { | ||
?s rdf:type ?t . | ?s rdf:type ?t . | ||
} | } | ||
LIMIT 50 | LIMIT 50 | ||
Line 37: | Line 37: | ||
===URI for Tim Berners-Lee=== | ===URI for Tim Berners-Lee=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
PREFIX foaf: <http://xmlns.com/foaf/0.1/> | PREFIX foaf: <http://xmlns.com/foaf/0.1/> | ||
Line 49: | Line 49: | ||
===Describe Tim Berners-Lee=== | ===Describe Tim Berners-Lee=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
DESCRIBE <http://i2s.uib.no/kg4news/author/1432678629> | DESCRIBE <http://i2s.uib.no/kg4news/author/1432678629> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 55: | Line 55: | ||
===Papers that mention "Semantic Web" in the title=== | ===Papers that mention "Semantic Web" in the title=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
PREFIX ss: <http://semanticscholar.org/> | PREFIX ss: <http://semanticscholar.org/> | ||
Line 67: | Line 67: | ||
===Similar filter that ignores capitalisation (lower/upper case)=== | ===Similar filter that ignores capitalisation (lower/upper case)=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
FILTER(REGEX(STR(?title), "Semantic Web", "i")) | FILTER(REGEX(STR(?title), "Semantic Web", "i")) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Authors sorted by name=== | ===Authors sorted by name=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
PREFIX foaf: <http://xmlns.com/foaf/0.1/> | PREFIX foaf: <http://xmlns.com/foaf/0.1/> | ||
Line 80: | Line 80: | ||
ORDER BY ?name | ORDER BY ?name | ||
LIMIT 10 | LIMIT 10 | ||
</syntaxhighlight> | |||
===Sorted by descending name=== | |||
<syntaxhighlight lang="SPARQL"> | |||
ORDER BY DESC(?name) | |||
</syntaxhighlight> | |||
===Count papers by author=== | |||
<syntaxhighlight lang="SPARQL"> | |||
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |||
PREFIX dct: <http://purl.org/dc/terms/> | |||
PREFIX ss: <http://semanticscholar.org/> | |||
SELECT DISTINCT ?author (COUNT(?paper) AS ?count) WHERE { | |||
?author rdf:type ss:Author . | |||
?paper rdf:type ss:Paper ; | |||
dct:contributor ?author . | |||
} | |||
GROUP BY ?author | |||
LIMIT 10 | |||
</syntaxhighlight> | |||
===Only list the most prolific authors=== | |||
<syntaxhighlight lang="SPARQL"> | |||
<syntaxhighlight lang="SPARQL"> | |||
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |||
PREFIX dct: <http://purl.org/dc/terms/> | |||
PREFIX ss: <http://semanticscholar.org/> | |||
SELECT DISTINCT ?author (COUNT(?paper) AS ?count) WHERE { | |||
?author rdf:type ss:Author . | |||
?paper rdf:type ss:Paper ; | |||
dct:contributor ?author . | |||
} | |||
GROUP BY ?author | |||
HAVING (?count >= 10) # similar to a filter expression | |||
LIMIT 10 # include limit when you test | |||
</syntaxhighlight> | |||
===Order by descending paper count=== | |||
<syntaxhighlight lang="SPARQL"> | |||
SELECT ... { | |||
... | |||
} | |||
GROUP BY ?person | |||
HAVING (?count > 10) | |||
ORDER BY DESC(?count) | |||
LIMIT 10 | |||
</syntaxhighlight> | |||
===Order by descending paper count and then by author name=== | |||
<syntaxhighlight lang="SPARQL"> | |||
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |||
PREFIX dct: <http://purl.org/dc/terms/> | |||
PREFIX foaf: <http://xmlns.com/foaf/0.1/> | |||
PREFIX ss: <http://semanticscholar.org/> | |||
SELECT DISTINCT ?person (SAMPLE(?name) AS ?name) (COUNT(?paper) AS ?count) WHERE { | |||
?person rdf:type ss:Author ; | |||
foaf:name ?name . | |||
?paper rdf:type ss:Paper ; | |||
ss:title ?title ; | |||
dct:contributor ?person . | |||
} | |||
GROUP BY ?person | |||
HAVING (?count > 10) | |||
ORDER BY DESC(?count) | |||
LIMIT 10 | |||
</syntaxhighlight> | |||
===Embedded Wikidata query=== | |||
<syntaxhighlight lang="sparql"> | |||
PREFIX foaf: <http://xmlns.com/foaf/0.1/> | |||
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> | |||
SELECT DISTINCT ?enname ?person ?wdperson WHERE { | |||
BIND("T. Berners-Lee"@en AS ?enname) | |||
SERVICE <https://query.wikidata.org/bigdata/namespace/wdq/sparql> { | |||
SELECT ?wdperson ?enname WHERE { | |||
?wdperson skos:altLabel ?enname . | |||
} | |||
LIMIT 1 | |||
} | |||
BIND(STR(?enname) AS ?name) | |||
?person foaf:name ?name . | |||
} | |||
LIMIT 1 | |||
</syntaxhighlight> | |||
===Limit=== | |||
<syntaxhighlight lang="SPARQL"> | |||
</syntaxhighlight> | |||
===Limit=== | |||
<syntaxhighlight lang="SPARQL"> | |||
</syntaxhighlight> | |||
===Limit=== | |||
<syntaxhighlight lang="SPARQL"> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
===Limit=== | ===Limit=== | ||
<syntaxhighlight> | <syntaxhighlight lang="SPARQL"> | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 14:39, 31 January 2023
Lecture 3:SPARQL
The KG4News knowledge graph can be accessed here (namespace kb) (read-only). To test updates, you can run your own Blazegraph server or try the I2S sandbox.
Limit
SELECT ?p WHERE {
?s ?p ?o .
}
LIMIT 10
List distinct properties only (with limit)
SELECT DISTINCT ?p WHERE {
?s ?p ?o .
}
LIMIT 10
Limit with offset
SELECT DISTINCT ?p WHERE {
?s ?p ?o .
}
LIMIT 10 OFFSET 9
List rdf:types
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?t WHERE {
?s rdf:type ?t .
}
LIMIT 50
URI for Tim Berners-Lee
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?person WHERE {
?person foaf:name ?name .
FILTER(CONTAINS(?name, "Berners-Lee"))
}
LIMIT 10 # best to use limit if something goes wrong
Describe Tim Berners-Lee
DESCRIBE <http://i2s.uib.no/kg4news/author/1432678629>
DESCRIBE returns a new RDF graph, whereas SELECT returns a table of rows.
Papers that mention "Semantic Web" in the title
PREFIX ss: <http://semanticscholar.org/>
SELECT DISTINCT ?paper ?title WHERE {
?paper ss:title ?title .
FILTER(CONTAINS(STR(?title), "Semantic Web"))
}
LIMIT 50
Similar filter that ignores capitalisation (lower/upper case)
FILTER(REGEX(STR(?title), "Semantic Web", "i"))
Authors sorted by name
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT * WHERE {
?author foaf:name ?name .
}
ORDER BY ?name
LIMIT 10
Sorted by descending name
ORDER BY DESC(?name)
Count papers by author
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX ss: <http://semanticscholar.org/>
SELECT DISTINCT ?author (COUNT(?paper) AS ?count) WHERE {
?author rdf:type ss:Author .
?paper rdf:type ss:Paper ;
dct:contributor ?author .
}
GROUP BY ?author
LIMIT 10
Only list the most prolific authors
<syntaxhighlight lang="SPARQL">
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX ss: <http://semanticscholar.org/>
SELECT DISTINCT ?author (COUNT(?paper) AS ?count) WHERE {
?author rdf:type ss:Author .
?paper rdf:type ss:Paper ;
dct:contributor ?author .
}
GROUP BY ?author
HAVING (?count >= 10) # similar to a filter expression
LIMIT 10 # include limit when you test
Order by descending paper count
SELECT ... {
...
}
GROUP BY ?person
HAVING (?count > 10)
ORDER BY DESC(?count)
LIMIT 10
Order by descending paper count and then by author name
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ss: <http://semanticscholar.org/>
SELECT DISTINCT ?person (SAMPLE(?name) AS ?name) (COUNT(?paper) AS ?count) WHERE {
?person rdf:type ss:Author ;
foaf:name ?name .
?paper rdf:type ss:Paper ;
ss:title ?title ;
dct:contributor ?person .
}
GROUP BY ?person
HAVING (?count > 10)
ORDER BY DESC(?count)
LIMIT 10
Embedded Wikidata query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?enname ?person ?wdperson WHERE {
BIND("T. Berners-Lee"@en AS ?enname)
SERVICE <https://query.wikidata.org/bigdata/namespace/wdq/sparql> {
SELECT ?wdperson ?enname WHERE {
?wdperson skos:altLabel ?enname .
}
LIMIT 1
}
BIND(STR(?enname) AS ?name)
?person foaf:name ?name .
}
LIMIT 1