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
14
votes
3 answers

Selecting some distinct and some not-distinct tags in SPARQL

I'm trying to query DBPedia for a list of properties relating to a given class in the ontology, but since the human-readable "labels" aren't always clear, I'd also like to provide an example from the database. The problem is that while I want to…
Paul
  • 9,014
  • 9
  • 41
  • 75
14
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
  • 788
  • 1
  • 9
  • 30
14
votes
3 answers

SPARQL DESCRIBE query

Seems I don't grok SPARQL DESCRIBE queries. I need to retrieve the full graphs of resources matching a condition. On one SPARQL endpoint I have tried (Norwegian Rådata Nå, http://data.bibsys.no/data/query_authority.html) this works just fine: PREFIX…
Nils Weinander
  • 2,019
  • 1
  • 14
  • 19
14
votes
3 answers

Sparql: how to GROUP BY More Than One Column

In SPARQL, we can group the rows by a column through the gollowing syntax: GROUP BY ?colName Can we group by more than 1 columns eg: GROUP BY (?colName1 + ?colName2 + ?colName3) Suppose a query like: Select ?a ?b ?c (MIN(?y) AS ?d) Where…
sapthrishi007
  • 363
  • 3
  • 12
13
votes
4 answers

Is it possible to visualize the output of a graph query (Gremlin or SPARQL) as nodes and edges in Amazon Neptune?

GREMLIN and SPARQL only define the APIs for graph queries. How do I use the API responses and and plot that as an actual graph, with edges and vertices? Is there something like MySQL Workbench for graphs?
The-Big-K
  • 2,304
  • 14
  • 32
13
votes
1 answer

Filter by date range in SPARQL

I am using Jena's SPARQL engine and trying to write a query to filter on a date range as I need to find the value of a property after a fixed date. My date property is in the following format: Fri May 23 10:20:13 IST 2014 How do I write a SPARQL…
cooljohny
  • 596
  • 3
  • 11
  • 29
13
votes
6 answers

How to bind a variable to a queried item in SPARQL

In this simple sparql query I get a list of subjects whose object is 42 SELECT ?v WHERE { ?v ?p 42 } If I add ?p as a variable SELECT ?v ?p WHERE { ?v ?p 42 } I will get two entities per row, the subject and the predicate. What if I wanted three…
Stefano Borini
  • 125,999
  • 87
  • 277
  • 404
13
votes
4 answers

Specify multiple rdf:types in a SPARQL query

I have a SPARQL query like this PREFIX prefix: SELECT * WHERE { ?x rdf:type ?type . } Suppose now I want to specify the type of ?type as being either prefix:type1 or prefix:type2; how should this be done?
Noor
  • 18,061
  • 35
  • 123
  • 236
13
votes
6 answers

Reverse wikipedia geotagging lookup

Wikipedia is geotagging a lot of its articles. (Look in the top right corner of the page.) Is there any API for querying all geotagged pages within a specified radius of a geographical position? Update Okay, so based on lost-theory's answer I tried…
Bjarke Freund-Hansen
  • 23,930
  • 22
  • 87
  • 133
13
votes
2 answers

SPARQL 1.1: how to use the replace function?

How can one use the replace function in SPARQL 1.1, especially in update commands? For example, if I have a number of triples ?s ?p ?o where ?o is a string and for all triples where ?o contains the string "gotit" I want to insert an additional…
jpp1
  • 1,585
  • 2
  • 16
  • 35
12
votes
3 answers

How to recursively expand blank nodes in SPARQL construct query?

There is probably an easy to answer to this, but I can't even figure out how to formulate the Google query to find it. I'm writing SPARQL construct queries against a dataset that includes blank nodes. So if I do a query like CONSTRUCT {?x ?y ?z…
rogueleaderr
  • 4,084
  • 2
  • 28
  • 40
12
votes
2 answers

Example python script that uses DBPedia?

I am writing a python script to extract "Entity names" from a collection of thousands of news articles from a few countries and languages. I would like to make use of the amazing DBPedia structured knwoledge, say for example to look up the names of…
jaz
  • 155
  • 1
  • 7
12
votes
1 answer

How get DBpedia data in a specific language?

i tried this request http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=film&QueryString=transformers&MaxHits=1 but if i want to retrieve info in italian language? it there another service (similar this) that can?
Leonardo
  • 2,033
  • 6
  • 28
  • 32
12
votes
1 answer

Querying DBpedia for English-only description (with SPARQL)

I'm querying dbpedia.org for a description of Big Ben with this SPARQL query: select ?desc where { ?desc } This returns a list of descriptions in at least 10…
siamii
  • 20,540
  • 26
  • 86
  • 136
12
votes
4 answers

How do I get started with Sparql as a .NET Developer?

I'm trying to parse Project Gutenberg's large RDF file. Another member of my team is pretty stuck, having tried Semweb and a python library. Being a little naive about rdf, I tried to write a plain ol' xml parsing script in Ruby. I soon realized…
rsteckly
  • 1,882
  • 3
  • 22
  • 34