O'Reilly logo

Learning SPARQL, 2nd Edition by Bob DuCharme

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


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 that use this data model make it possible to expose diverse sets of data (including, as we’ll see, relational databases) with a common, standardized interface. Accessing this data doesn’t require learning new APIs because both open source and commercial software (including Oracle 11g and IBM’s DB2) are available with SPARQL support that lets you take advantage of these data sources. Because of this data and tool availability, SPARQL has let people access a wide variety of public data and has provided easier integration of data silos within many enterprises.

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 that retrieved data. Once you can do this, you can take advantage of the extensive choice of tools and application libraries that use SPARQL to retrieve, update, and mix and match the huge amount of RDF-accessible data out there.

Organization of This Book

You don’t have to read this book cover-to-cover. After you read Chapter 1, feel free to skip around, although it might be easier to follow the later chapters if you begin by reading at least through Chapter 5.

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, Query Efficiency and Debugging

Things to keep in mind that can help your queries run more efficiently as you work with growing volumes of data

Chapter 8, Working with SPARQL Query Result Formats

How your applications can take advantage of the XML, JSON, CSV, and TSV formats defined by the W3C for SPARQL processors to return query results

Chapter 9, RDF Schema, OWL, and Inferencing

How SPARQL can take advantage of the metadata that RDF Schemas, OWL ontologies, and SPARQL rules can add to your data

Chapter 10, Building Applications with SPARQL

Different roles that SPARQL can play in applications that you develop

Chapter 11, A SPARQL Cookbook

A set of SPARQL queries and update requests that can be useful in a wide variety of situations


A glossary of terms and acronyms used when discussing SPARQL and RDF technology

You’ll find an index at the back of the book to help you quickly locate explanations for SPARQL and RDF keywords and concepts. The index also lets you find where in the book each sample file is used.

Conventions Used in This Book

The following typographical conventions are used in this book:


Indicates new terms, URLs, email addresses, 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, datatypes, 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 shown in the same monospace 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: <http://learningsparql.com/ns/demo#> 
SELECT ?person
{ ?person d:homeTel "(229) 276-5135" . }

When including punctuation at end of a quoted phrase, this book has 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.


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

You’ll find a ZIP file of all of this book’s sample code and data files at http://www.learningsparql.com, along with links to free SPARQL software and other resources.

This book is here to help you get your job done. In general, if this book includes code examples, you may use the code 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, 2nd edition, by Bob DuCharme (O’Reilly). Copyright 2013 O’Reilly Media, 978-1-449-37143-2.”

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

Safari® Books Online


Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

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 http://oreil.ly/learn-sparql-2e.

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 http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia


For their excellent contributions to the first edition, 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 the second edition, I received many great suggestions from Rob Vesse, Gary King, Matthew Gibson, and Christine Connors; Andy also reviewed some of the new material on its way into the book.

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, Kristen Borg, Amanda Kersey, 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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required