Questions tagged [sparql]

SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is a set of specifications by W3C that provide languages and protocols to query and manipulate RDF graph content on the Web or in an RDF store.

SPARQL

SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is a set of specifications by W3C that provide languages and protocols to query and manipulate RDF graph content on the Web or in an RDF store.

SPARQL 1.0

SPARQL 1.0 is the original version of SPARQL, and simply provides a query language for RDF. The language is based on Graph Pattern matching and provides 4 forms of query:

  1. ASK WHERE { } - An ASK query simply asks whether there exists a match for the Graph Pattern stated in the WHERE clause in the data being queried.
    This returns a Boolean SPARQL Results Set containing a True/False response.
  2. SELECT * WHERE { } - A SELECT query finds all the solutions that match the Graph Pattern and returns the desired parts of them. Results can be ORDERed as desired and use LIMIT and/or OFFSET for paging purposes. This is the most commonly used query form and corresponds in function (if very differently in syntax and semantics) to the SQL that many developers coming to the Semantic Web are familiar with.
    This returns a SPARQL Result Set containing the solutions.
  3. DESCRIBE <http://example.org> - A DESCRIBE query gets the description of one/more resources from the data. The query engine is free to decide what constitutes a description. A WHERE clause may be used to select what resources are to be described.
    This returns an RDF Graph.
  4. CONSTRUCT { } WHERE { } - A CONSTRUCT query takes solutions that match the WHERE clause and uses them to construct a new RDF Graph.
    This returns an RDF Graph.

SPARQL 1.0 Example

A SPARQL 1.0 query might look like the following:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
FROM <http://default>
WHERE
{
  ?s a ?type .
  OPTIONAL
  {
    ?s rdfs:label ?label .
    FILTER (LANGMATCHES(?label, "en"))
  }
}
ORDER BY ?label
LIMIT 10

This query looks for things with a type in the graph <http://default> and optionally includes their labels provided those labels are in English. It orders the results by the label limiting the results returned to 10.

SPARQL 1.1

SPARQL 1.1 is a major extension to the SPARQL ecosystem approved as a W3C Recommendation in March 2013. It provides many extensions to the existing query language including:

  • Project expressions in SELECT, e.g. (?x + ?y AS ?z)
  • Aggregates, e.g. COUNT(), GROUP BY, and HAVING
  • Property Paths, e.g. {?x ex:predicate+ ?y}
  • EXISTS and NOT EXISTS filters
  • MINUS clause for subtractive negation
  • SERVICE clause for federated querying
  • Subqueries
  • Many new built in functions particularly around string and date manipulation

It also adds a number of entirely new features into the ecosystem including:

See the SPARQL 1.1 Implementation Report for implementations that have reported compliance test results. See the SPARQL Wikipedia article for examples, extensions, and another list of implementations.

6076 questions
22
votes
4 answers

How to query Wikidata items using its labels?

How can I query Wikidata to get all items that have labels contain a word? I tried this but didn't work; it retrieved nothing. SELECT ?item ?itemLabel WHERE { SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?item…
fattah.safa
  • 926
  • 2
  • 14
  • 36
22
votes
2 answers

Where do I test my queries for my RDF written in SPARQL

I am a beginner with Semantic Web technologies, My question might be a very basic one but I am really stuck figuring it out. I have a RDF file I created from an XML and have validated it using w3.org RDF VALIDATOR. My question is how can I run…
Yaba
  • 304
  • 1
  • 3
  • 15
21
votes
2 answers

Get all properties for a DBpedia class

How to get a list of properties for a specific class? Consider the class dbpedia-owl:Person. All instances of the Person class have some properties prefixed with dbpprop:. How can I get all the dbpprop: properties that we may find for all the…
user878812
  • 349
  • 1
  • 2
  • 6
21
votes
1 answer

SPARQL property path queries with arbitrary properties

SPARQL property path queries of arbitrary length require using specific properties. I want to query and find any path starting from a resource and ending in another resource. For example: SELECT ?p WHERE { :startNode ?p* :endNode } where ?p*…
user3287385
  • 213
  • 2
  • 5
21
votes
2 answers

Select multiple values as object in SPARQL pattern

In SPARQL we can do something like this select * where { ?s (_:prop1 | _:prop2) "some_val" . ... #use ?s in other patterns ?s ?o ?p . } Is it possible to do the same for the object part of the pattern? And what are some…
0xFF
  • 4,140
  • 7
  • 41
  • 58
20
votes
3 answers

SPARQL select optional with language

I have some triples that look like this: test:thing rdfs:label "Non-Language Label" test:thing rdfs:label "English Label"@en test:thing rdfs:label "French Label"@fr I'd like to form a sparql query that gives me the "Non-Language Label" AND the…
Devin McQueeney
  • 1,277
  • 2
  • 14
  • 31
19
votes
3 answers

SPARQL: Get all the entities of subclasses of a certain class

I've to get all the instances of a class C and subclasses (direct or indirect) of C, in SPARQL. I can get all the direct subclasses of C in this way: SELECT ?entity WHERE { ?subclass rdfs:subClassOf :C . ?entity rdf:type ?subclass . } But I…
auino
  • 1,644
  • 5
  • 23
  • 43
19
votes
1 answer

Extract triples containing particular substring using SPARQL

I want to extract a triple which contains word say "alice" in its subject. The query I used was: SELECT ?s ?p ?o WHERE { ?s ?p ?o .FILTER regex(?s, \"alice\") .} This doesn't give me any results inspite of have a triple which satisfies this…
user2335580
  • 398
  • 1
  • 4
  • 16
18
votes
3 answers

Querying WikiData, difference between p and wdt default prefix

I am new to wikidata and I can't figure out when I should use --> wdt prefix (http://www.wikidata.org/prop/direct/) and when I should use --> p prefix (http://www.wikidata.org/prop/). in my sparql queries. Can someone explain what each of these…
Bahar
  • 770
  • 5
  • 18
18
votes
3 answers

The difference between blank nodes and variables in SPARQL queries

I've studied SPARQL specification on the topic and also found this answer rather interesting. However definitions are complicated enough, so I still don't see the answer for my question. I can't find any example of query with blank nodes that…
Andy White
  • 183
  • 1
  • 5
18
votes
1 answer

Binding a variable to one of two values with IF?

In the following SPARQL query, I'm not sure how to use if to bind one of two strings to the variable ?result. I heard that there are concepts of “in scope” and “out of scope,” but I don't really see the difference. I've also tried putting the if…
user2983384
  • 199
  • 1
  • 2
  • 5
17
votes
1 answer

SPARQL - Restricting Result Resource to Certain Namespace(s)

Is there a standard way of restricting the results of a SPARQL query to belong to a specific namespace.
myahya
  • 3,079
  • 7
  • 38
  • 51
17
votes
2 answers

How to get all companies from DBPedia?

I'm new to querying DBPedia. How can I get all companies from http://dbpedia.org/sparql? This query returns only 50'000 organizations: SELECT DISTINCT * WHERE {?company a dbpedia-owl:Company}
Anton
  • 494
  • 5
  • 19
17
votes
2 answers

Selecting strings with LIKE operator in SPARQL

In ANSI SQL, you can write something like: SELECT * FROM DBTable WHERE Description LIKE 'MEET' or also: SELECT * FROM DBTable WHERE Description LIKE '%MEET%' What I would like help with is writing the SPARQL equivalent of the above please.
Kobojunkie
  • 6,375
  • 31
  • 109
  • 164
17
votes
2 answers

How to write SPARQL query that efficiently matches string literals while ignoring case

I am using Jena ARQ to write a SPARQL query against a large ontology being read from Jena TDB in order to find the types associated with concepts based on rdfs label: SELECT DISTINCT ?type WHERE { ?x
lmsurprenant
  • 1,723
  • 2
  • 14
  • 28