Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active June 4, 2023 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knbknb/80dda962d78c4cc4957f69d6111332b2 to your computer and use it in GitHub Desktop.
Save knbknb/80dda962d78c4cc4957f69d6111332b2 to your computer and use it in GitHub Desktop.
SPARQL: Basic queries
# show all graphs accessiblefrom a SPARQL endpoint
# no "base" prefix is needed
#
#
# from anzograph tutorial.
# query works in anzograph running on localhost,
# but not on dbpedia.org, wikidata and other public endpoints
# on dbpedia running on localhost:8890/sparql this returns 54261 rows
SELECT DISTINCT ?graph
WHERE {
GRAPH ?graph { ?s ?p ?o . }
}
# from teh anzograph tutorial
# load ttl files quickly
LOAD WITH 'global'
<s3://csi-notebook-datasets/MovieTicketAnalysis/20190217/tickit.ttl.gz>
INTO GRAPH <tickit>
LOAD
<file:/var/tmp/shared-files/GettingStarted_Music_Data.ttl>
INTO GRAPH <music>
LOAD
<file:/var/tmp/shared-files/GettingStarted_Movie_Schema.ttl.gz>
INTO GRAPH <movies>
base <http://localhost:8180/sparql/>
# this example uses the <tickit> graph from the anzograph tutorial
# calculate graph density
SELECT (?nrEdges/(?nrNodes *(?nrNodes - 1.0)) AS ?graphDensity)
FROM <tickit>
WHERE {
{ SELECT (COUNT (*) AS ?nrEdges) (COUNT (DISTINCT ?person) AS ?nrNodes)
WHERE { ?person <friend> ?anotherPerson . }
}
}
# SELECT DISTINCT ?predicates
# FROM <tickit>
# WHERE {
# ?subject ?predicates ?object .
# }
# ORDER BY ?predicates
# LIMIT 100
# SELECT ?predicate (COUNT (?predicate) AS ?count)
# FROM <tickit>
# WHERE {
# ?s ?predicate ?o .
# }
# GROUP BY ?predicate
# ORDER BY DESC(?count)
# number of triples in the default graph, (or in beatles graph, if comment removed)
# on 2020-12-18,
# dbpedia SPARQL endpoint returned 9911000, in < 1 second
# wikidata: "Query timeout limit reached".
SELECT (COUNT(?s) as ?numTriples)
#FROM <beatles>
WHERE {
?s ?p ?o .
}
# number of distinct properties in the default graph, (or in beatles graph, if comment removed)
# on 2020-12-18,
# dbpedia SPARQL endpoint returned 1376000, in 1 second
# wikidata: returns 37914 rows.
SELECT ?predicate (COUNT(?predicate) as ?predicateCount)
# from <tickit>
WHERE {
?subject ?predicate ?object .
}
GROUP BY ?predicate
ORDER BY DESC(?predicateCount)
# wikidata: returns 37914 rows.
# schema:description 2382190418
# rdf:type 1281848697
# wikibase:rank 1169180184
# wikidata prefixes
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
# select exactly 1 specific item from wikidata
# a picture of a castle
# https://stackoverflow.com/questions/69388360/get-specific-item-from-wikidata-with-sparql
SELECT ?pic
WHERE
{
wd:Q1651322 wdt:P18 ?pic .
#FILTER(?item = wd:Q1651322)
SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
}
LIMIT 1
# DESCRIBE wd:Q22277104 # Teodor Bogdanov, Volleyball player
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment