It is hardly surprising that the science they turned to for an explanation of things was divination, the science that revealed connections between words and things, proper names and the deductions that could be drawn from them ...

Henri-Jean Martin, The History and Power of Writing

Why Learn SPARQL?

More and more people are using the query language SPARQL (pronounced “sparkle”) to pull data from a growing collection of public and private data. Whether this data is part of a semantic web project or an integration of two inventory databases on different platforms behind the same firewall, SPARQL is making it easier to access it. In the words of W3C Director and Web inventor Tim Berners-Lee, “Trying to use the Semantic Web without SPARQL is like trying to use a relational database without SQL.”

SPARQL was not designed to query relational data, but to query data conforming to the RDF data model. RDF-based data formats have not yet achieved the mainstream status that XML and relational databases have, but an increasing number of IT professionals are discovering that tools using the RDF data model let them expose diverse sets of data (including, as we’ll see, relational databases) with a common, standardized interface. Both open source and commercial software have become available with SPARQL support, so you don’t need to learn new programming language APIs to take advantage of these data sources. This data and tool availability has led to SPARQL letting people access a wide variety of public data and providing easier integration of data silos within an enterprise.

Although this book’s table of contents, glossary, and index let it serve as a reference guide when you want to look up the syntax of common SPARQL tasks, it’s not a complete reference guide—if it covered every corner case that might happen when you use strange combinations of different keywords, it would be a much longer book. Instead, the book’s primary goal is to quickly get you comfortable using SPARQL to retrieve and update data and to make the best use of the retrieved data. Once you can do this, you can take advantage of the extensive choice of tools and application libraries that use this query language to retrieve, update, and mix and match the huge amount of RDF-accessible data out there.

Organization of This Book

Chapter 1, Jumping Right In: Some Data and Some Queries

Writing and running a few simple queries before getting into more detail on the background and use of SPARQL.

Chapter 2, The Semantic Web, RDF, and Linked Data (and SPARQL)

The bigger picture: the semantic web, related specifications, and what SPARQL adds to and gets out of them.

Chapter 3, SPARQL Queries: A Deeper Dive

Building on Chapter 1, a broader introduction to the query language.

Chapter 4, Copying, Creating, and Converting Data (and Finding Bad Data)

Using SPARQL to copy data from a dataset, to create new data, and to find bad data.

Chapter 5, Datatypes and Functions

How datatype metadata, standardized functions, and extension functions can contribute to your queries.

Chapter 6, Updating Data with SPARQL

Using SPARQL’s update facility to add to and change data in a dataset instead of just retrieving it.

Chapter 7, Building Applications with SPARQL: A Brief Tour

How you can incorporate SPARQL queries into web-based applications.


A glossary of terms and acronyms used when discussing SPARQL and the semantic web.

You’ll also find an index at the back of the book to help you quickly locate explanations for SPARQL and RDF keywords and concepts.

Conventions Used in This Book

The following typographical conventions are used in this book:


Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Documentation Conventions

Variables and prefixed names are written in a monospace font like this. (If you don’t know what prefixed names are, you’ll learn in Chapter 2.) Sample data, queries, code, and markup are also shown in this font. Sometimes these include bolded text to highlight important parts that the surrounding discussion refers to, like the quoted string in the following:

# filename: ex001.rq

PREFIX d: <> 
SELECT ?person
{ ?person d:homeTel "(229) 276-5135" . }

When including punctuation at end of a quoted phrase, I put it inside the quotation marks in the American publishing style, “like this,” unless the quoted string represents a specific value that would be changed if it included the punctuation. For example, if your password on a system is “swordfish”, I don’t want you to think that the comma is part of the password.

The following icons alert you to details that are worth a little extra attention:


An important point that might be easy to miss or a tip that can make your development or your queries more efficient.


A tip that can make your development or your queries more efficient.


A warning about a common problem or an easy trap to fall into.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning SPARQL by Bob DuCharme (O’Reilly). Copyright 2011 Bob DuCharme, 978-1-449-30659-5.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

You’ll also find a zip file of all of this book’s sample code and data files at, along with links to free SPARQL software and other resources.

Safari® Books Online


Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website at

Find us on Facebook:

Follow us on Twitter:

Watch us on YouTube:


For their excellent contributions to the great improvements made to this book in the last two months, I’d like to thank the book’s technical reviewers (Dean Allemang, Andy Seaborne, and Paul Gearon) and sample audience reviewers (Priscilla Walmsley, Eric Rochester, Peter DuCharme, and David Germano).

For helping me to get to know SPARQL well, I’d like to thank my colleagues at TopQuadrant: Irene Polikoff, Robert Coyne, Ralph Hodgson, Jeremy Carroll, Holger Knublauch, Scott Henninger, and the aforementioned Dean Allemang.

I’d also like to thank Dave Reynolds and Lee Feigenbaum for straightening out some of the knottier parts of SPARQL for me, and O’Reilly’s Simon St. Laurent, Sarah Schneider, Sanders Kleinfeld, and Jasmine Perez for helping me turn this into an actual book.

Mostly, I’d like to thank my wife Jennifer and my daughters Madeline and Alice for putting up with me as I researched and wrote and tested and rewrote and rewrote this.

Get Learning SPARQL now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.