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.

5278 questions
20
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
  • 284
  • 3
  • 15
18
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
  • 3,980
  • 6
  • 38
  • 57
17
votes
2 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,522
  • 5
  • 19
  • 41
17
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
  • 173
  • 1
  • 5
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
  • 505
  • 4
  • 16
17
votes
1 answer

Aggregating results from SPARQL query

I'm querying a dataset of tweets: SELECT * WHERE { ?tweet smo:tweeted_at ?date ; smo:has_hashtag ?hashtag ; smo:tweeted_by ?account ; smo:english_tweet true . FILTER ( ?date >= "20130722"^^xsd:date && ?date <…
betseyb
  • 1,162
  • 2
  • 16
  • 34
16
votes
2 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
  • 163
  • 1
  • 5
16
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
  • 350
  • 1
  • 3
  • 13
16
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
  • 179
  • 1
  • 1
  • 5
16
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,236
  • 1
  • 9
  • 20
15
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
  • 2,889
  • 7
  • 34
  • 50
15
votes
1 answer

Sparql Query to get all the possible movies available from dbpedia

To get all the possible film name, I used sparql query: PREFIX rdfs: PREFIX rdfs: SELECT DISTINCT ?film_title ?film_abstract WHERE { ?film_title rdf:type…
Shruts_me
  • 783
  • 1
  • 10
  • 22
15
votes
1 answer

How to properly use SPARQL OPTIONAL to retrieve attributes for a resource that may exist?

I'm trying to use a SPARQL query to retrieve information about a DBpedia resource (a Person). I'd like to use the same query to retrieve data about any Person by parameterizing the resource URI. Since some attributes may not exist for a particular…
15
votes
1 answer

Comments in SPARQL query?

Can I write comments in a query, so that only God but also me remember what I was doing? I just started learning SPARQL, so forgive my ignorance. For example: SELECT ?names WHERE { ?names dbo:award :Turing_Award // get names that won Turing…
gsamaras
  • 66,800
  • 33
  • 152
  • 256
15
votes
1 answer

CONSTRUCT into a named graph

I am attempting to use a SPARQL Construct query to create a new named graph from an existing one. The database I am querying contains http://graph.com/old as an existing named graph. I am using Jena TDB as the database, accessed through a Jena…
Yoav Zimmerman
  • 468
  • 4
  • 10