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
12
votes
2 answers

Downloading GeoJSON boundaries using SPARQL from publicly available data

I'm interested in downloading some boundary files from statistics.gov.scot, which is an official statistical repository for sharing statistical data that utilises SPARQL queries. Background Statistics.gov.scot provides access to GeoJSON boundaries…
Konrad
  • 14,406
  • 15
  • 86
  • 141
12
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
  • 165
  • 1
  • 12
12
votes
1 answer

DISTINCT only on one value with SPARQL

I want to retrieve with SPARQL the list of the italian cities with more than 100k of population and I'm using the following query: PREFIX dbo: SELECT ?city ?name ?pop WHERE { ?city a dbo:Settlement . ?city…
drstein
  • 1,071
  • 1
  • 11
  • 23
12
votes
1 answer

SPARQL using subquery with limit

I am developing a java application that uses ARQ to execute SPARQL queries using a Fuseki endpoint over TDB. The application needs a query that returns the place of birth of each person and other person that was born in the same place. To start, I…
Diego
  • 123
  • 1
  • 1
  • 5
12
votes
3 answers

Bind Multiple SPARQL Variables into One

Is there a way of doing something like this: BIND((?s1, ?s2, ?s3) AS ?s) such that queries on ?s will be distributed to the list? EDIT BTW, it seems the following does not work. Am I doing something wrong? SELECT * WHERE { VALUES (?s1 ?s2 ?s3)…
Bondolin
  • 2,241
  • 6
  • 25
  • 51
12
votes
2 answers

Matching of strings in SPARQL?

In ANSI SQL, you can write something like: Select * From DBTable Where DBTable.Description LIKE "MEET" or Select * From DBTable Where DBTable.Description LIKE "%MEET%" What I would like help with is writing the SPARQL equivalent of the above…
Kobojunkie
  • 5,739
  • 30
  • 99
  • 162
11
votes
2 answers

How do I limit the number of results for a specific variable in a SPARQL query?

Let's say I have a SPARQL query like this, looking for resources that have some shared property with a focal resource, and also getting some other statements about the focal resource : CONSTRUCT { ?focal pred:icate ?shared . ?other pred:icate…
treat your mods well
  • 2,556
  • 1
  • 23
  • 33
11
votes
1 answer

Retrieving a DBpedia resource by its string name with SPARQL and without knowing its type

As shown in this question which has a similar title, I would like to retrieve a dbpedia resource by knowing a part of its name. I'm a beginner when it comes to SPARQL and such, but the example in the question helped me a lot, as the author searched…
Ged ort
  • 113
  • 1
  • 5
11
votes
3 answers

Is there an API for searching Stock Symbols based on Company Names?

This is a similar question to Stock ticker symbol lookup API however that was never answered to the standard I think is needed for a commercial application. The problem is this: Given a company name, partial name, or stock symbol, return a list of…
danpalmer
  • 2,106
  • 3
  • 23
  • 41
11
votes
3 answers

How to skip bad dates in DBpedia SPARQL request?

I need to get data about films from DBpedia. I use SPARQL query as follows on http://dbpedia-live.openlinksw.com/sparql: PREFIX rdfs: PREFIX rdf: PREFIX foaf:…
11
votes
2 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,238
  • 1
  • 11
  • 28
11
votes
1 answer

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
  • 143
  • 7
11
votes
2 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
  • 621
  • 3
  • 16
11
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
  • 145
  • 1
  • 7
11
votes
2 answers

SPARQL Calculating the difference between two date

What is the function to calculate difference between two SPARQL xsd:dateTime ? I have found a reference to datediff, but this is not working. Does such a built in function exist in SPARQL as it does with most other query languages?
Shamim Hafiz
  • 19,616
  • 36
  • 104
  • 164