What is SPARQL?

by David Wood, Marsha Zaidman, Luke Ruth, and Michael Hausenblas, authors of Linked Data

SPARQL is the query language for RDF and Linked Data. SPARQL is to RDF data as the Structured Query Language (SQL) is to a relational database. SPARQL's name is nicely pronounceable and sounds interesting and fresh. SPARQL is interesting and fresh. This article, based on chapter 5 from Linked Data, shows you why.

SPARQL's name looks like an acronym, but the truth is the acronym was reverse engineered after the fact. The SPARQL Protocol and RDF Query Language is a recursive acronym in the tradition of the GNU (GNU's Not Unix) Project.

Like SQL, SPARQL is based on a widely implemented standard but various vendors have extended the language to suit themselves or show off particular features of their products. This article focuses on the standard language components. That is generally appropriate in any case: SPARQL implementations have not (yet) fragmented as much as SQL implementations.

NOTE The SPARQL Query Language for RDF is a rich language and far too complicated to describe in a single article. All the details may be found in the W3C Recommendation that describes SPARQL in detail, at http://www.w3.org/TR/rdf-sparql-query/. SPARQL version 1.1 is likely to become a W3C Recommendation shortly after this book is published; the specification for SPARQL version 1.1 is available at http://www.w3.org/TR/sparql11-query/.

Figure 1 W3C's SPARQL logo

SPARQL is defined by a family of W3C Recommendations and related Working Group Notes. The W3C SPARQL logo is shown in figure 1.

When you need SPARQL

SPARQL is best used when you want to query RDF graphs, as if one or more (possibly distributed) RDF graphs formed a database. Note that there are (many) native RDF databases, but you don't need to use one in order to query RDF using SPARQL.

Let's look at a sample query. We can query a real-world FOAF document for people that the FOAF document's owner knows.

Listing 1 SPARQL query to find FOAF friends

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

prefix foaf: <http://xmlns.com/foaf/0.1/>

select ?name ?url  

where {  

  ?person rdfs:seeAlso ?url ;