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
16
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
  • 10,381
  • 13
  • 48
  • 86
16
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,672
  • 16
  • 35
16
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…
Ganesh Kumaraswamy
  • 207
  • 1
  • 2
  • 7
16
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
  • 656
  • 5
  • 13
  • 31
16
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
  • 138,652
  • 96
  • 297
  • 431
16
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
  • 19,638
  • 38
  • 136
  • 254
16
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
  • 393
  • 3
  • 13
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
  • 843
  • 2
  • 12
  • 24
15
votes
2 answers

Wikidata results sorted by something similar to a PageRank

In Wikidata (Wikidata SPARQL endpoint), is there a way to order the SPARQL query results with something like a PageRank? SELECT DISTINCT ?entity ?entityLabel WHERE { ?entity wdt:P31 wd:Q5. SERVICE wikibase:label { bd:serviceParam…
jordipala
  • 193
  • 7
15
votes
2 answers

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
  • 588
  • 4
  • 11
14
votes
3 answers

Passing Multiple Arguments to GraphQL Query

First thing Appreciate this may be a bit of a stupid question, but I'm working with GraphQL having come from the RDF/Linked Data world and having a lot of trouble getting my head around how I would return a set. Essentially I want something where I…
Alex Lynham
  • 1,318
  • 2
  • 11
  • 29
14
votes
2 answers

What is the SPARQL query to get the name of all graphs existing in my triplestore?

I want to get the name of all existing graphs in my Fuseki server, it should return a message with a list of all graphs name.
user3457185
  • 175
  • 1
  • 1
  • 7
14
votes
3 answers

Getting readable results from Wikidata

Ok so I'm trying to get information from Wikidata about movies, take this movie for example: https://www.wikidata.org/wiki/Q24871 On the page the data is clearly displayed in a readable format, however when you trying to extract it via the API you…
Oliver at ontoit
  • 185
  • 1
  • 2
  • 13
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,081
  • 2
  • 15
  • 20
14
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
  • 2,019
  • 3
  • 22
  • 43