SPARQL

Name:

SPARQL 1.1 Query Language

Specifiation:

https://www.w3.org/TR/sparql11-query/

Methods supported:

HTTP GET and HTTP POST

Implementation platform:

OpenLink Virtuoso

RDF is a directed, labeled graph data format for representing information in the Web.

SPARQL query language allows to express queries across diverse RDF data stores.

SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions.

SPARQL also supports:

  • aggregation

  • subqueries

  • negation

  • creating values by expressions

  • extensible value testing

  • and constraining queries by source RDF graph.

The results of SPARQL queries can be result sets or RDF graphs.

SPARQL query using HTTP GET

For HTTP endpoints providing HTTP GET SPARQL protocol, we may send SPARQL queries as follows:

Having a query:

SELECT ?p
WHERE {?p ?s ?o}
LIMIT 2

one can send it to the SPARQL enabled endpoint https://lod.tamtamresearch.com/sparql using HTTP GET with the query being urlencoded in query query parameter:

GET /sparql?query=SELECT+%3Fp%0AWHERE+%7B%3Fp+%3Fs+%3Fo%7D%0ALIMIT+2&format=turtle HTTP/1.1
Host: lod.tamtamresearch.com

what could return result:

@prefix res: <http://www.w3.org/2005/sparql-results#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
_:_ a res:ResultSet .
_:_ res:resultVariable "p" .
@prefix xsd:    <http://www.w3.org/2001/XMLSchema#> .
_:_ res:solution [
      res:binding [ res:variable "p" ; res:value xsd:ID ] ] .
_:_ res:solution [
      res:binding [ res:variable "p" ; res:value xsd:date ] ] .

The format can be alternatively request by means of Accept header, e.g.:

GET /sparql?query=SELECT+%3Fp%0AWHERE+%7B%3Fp+%3Fs+%3Fo%7D%0ALIMIT+2 HTTP/1.1
Accept: text/turtle
Host: lod.tamtamresearch.com

SPARQL query using HTTP POST

Query text can be also sent in HTTP POST request body.

For query

SELECT ?p
WHERE {?p ?s ?o}
LIMIT 2

The HTTP request would be:

POST /sparql?format=text%2Fturtle HTTP/1.1
Content-Length: 35
Host: lod.tamtamresearch.com

SELECT ?p
WHERE {?p ?s ?o}
LIMIT 2

Similarly as with HTTP GET we may use header Accept to ask for specific result format:

POST /sparql HTTP/1.1
Accept: text/turtle
Content-Length: 35
Host: lod.tamtamresearch.com

SELECT ?p
WHERE {?p ?s ?o}
LIMIT 2

Supported output formats

Following values for format or Accept are supported:

  • text/turtle

  • application/rdf+xml

  • application/sparql-results+json

  • application/sparql-results+xml

  • text/csv

  • text/html

  • text/plain

  • text/tab-separated-values

Previous: Protocols | Next: HTTP GET