SPARQL Examples – Select

Find all triples

Use SELECT to signify you want to select certain information and WHERE to signify your conditions, restrictions, and filters. A LIMIT is used to avoid cracking the server on a large dataset.

SELECT ?subject ?predicate ?object WHERE {?subject ?predicate ?object} LIMIT 100

Find all same-subject triples by given subject

By specifying only the subject in the pattern, we can return all triples that have that subject. When an individual entity is defined by a given subject URI, then this is a way to get all the properties for that entity.

PREFIX rdfs: <> PREFIX rdf: <> SELECT ?subject ?predicate ?object WHERE { <> ?predicate ?object . } LIMIT 100

Since the subject is known, you could also omit ?subject from the SELECT line so that only the predicates and objects are returned.

Find and order most used predicates

SELECT ?predicate (COUNT(*)AS ?frequency) WHERE {?subject ?predicate ?object} GROUP BY ?predicate ORDER BY DESC(?frequency) LIMIT 10

Select the top 100 nodes in the RDF graph

RDF Rank is a GraphDB extension. It is similar to Page Rank and it identifies “important” nodes in an RDF graph based on their interconnectedness. It is accessed using the rank:hasRDFRank system predicate.

PREFIX rank:<> SELECT ?n WHERE {?n rank:hasRDFRank ?r } ORDER BY DESC(?r) LIMIT 100

Find anything with a label

The following query will find all triples where subject and object are joined by rdfs:label. In other words, anything that has been defined as having a label.

PREFIX rdfs: <> SELECT ?subject ?label WHERE { ?subject rdfs:label ?label } LIMIT 100

Find instances by class with a label

The following example query will get the labels of anything of the class type (rdf:type) Organization.

PREFIX rdfs: <> PREFIX rdf: <> SELECT ?subject ?label WHERE { ?subject rdf:type <> . ?subject rdfs:label ?label } LIMIT 100

Find all subjects with a given object property

PREFIX rdfs: <> PREFIX bc: <> SELECT ?subject WHERE { ?subject bc:hasEmployer <> } LIMIT 100

From my contacts ontology, I find all IBMers (i.e. the hasEmployer predicate points to the individual IBM, which is an object of type Organization).

Find all classes

PREFIX rdf: <> SELECT DISTINCT ?type WHERE { ?s a ?type. }

The SPARQL keyword a is a shortcut for the common predicate rdf:type, giving the class of a resource.

Find all classes with a given prefix

PREFIX bc: <> SELECT DISTINCT ?type WHERE { ?subject a ?type. FILTER( STRSTARTS(STR(?type),str(bc:)) ) }

The SPARQL keyword a is a shortcut for the common predicate rdf:type, giving the class of a resource.

Query from a particular graph on the web

PREFIX foaf: <> PREFIX card: <> SELECT ?homepage FROM <> WHERE { card:i foaf:knows ?known . ?known foaf:homepage ?homepage . }

The FROM keyword lets us specify the target graph in the query itself.
Of interest also in the query shown above:
By using ?known as an object of one triple and the subject of another, we traverse multiple links in the graph.

Find subjects with a property value greater than

Find me all landlocked countries with a population greater than 15 million

PREFIX rdfs: <> PREFIX type: <> PREFIX prop: <> SELECT ?country_name ?population WHERE { ?country a type:LandlockedCountries ; rdfs:label ?country_name ; prop:populationEstimate ?population . FILTER (?population > 15000000) . }

You can try this one at the DBPedia SPARQL endpoint.

  • FILTER constraints use boolean conditions to filter out unwanted query results.
  • Shortcut: a semicolon (;) can be used to separate two triple patterns that share the same subject. (?country is the shared subject above.)
  • rdfs:label is a common predicate for giving a human-friendly label to a resource.