ArticleslgStudy

science

SPARQL

SPARQL is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand SPARQL rather than just read about it. In short: SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language—that is, a semantic query language for databases—able to retrieve and manipulate data stored in Resource Description Framework (RDF) format. It was made a standard by the RDF Data Access Working Group (DAWG) of the World Wide Web Consortium, and is recognized as one of the key technologies of the se…

SPARQL — main illustration
SPARQL — illustration

Key takeaways

  • SPARQL belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect SPARQL to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of SPARQL from memory before moving on to harder problems.

Reference excerpt

SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language—that is, a semantic query language for databases—able to retrieve and manipulate data stored in Resource Description Framework (RDF) format. It was made a standard by the RDF Data Access Working Group (DAWG) of the World Wide Web Consortium, and is recognized as one of the key technologies of the semantic web. On 15 January 2008, SPARQL 1.0 was acknowledged by W3C as an official recommendation, and SPARQL 1.1 in March, 2013.

SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns. Implementations for multiple programming languages exist. There exist tools that allow one to connect and semi-automatically construct a SPARQL query for a SPARQL endpoint, for example ViziQuer. In addition, tools exist to translate SPARQL queries to other query languages, for example to SQL and to XQuery.

Features SPARQL allows users to write queries that follow the RDF specification of the W3C. Thus, the entire dataset is "subject-predicate-object" triples. Subjects and predicates are always URI identifiers, but objects can be URIs or literal values. This single physical schema of three "columns" is hypernormalized in that what would be one relational record with (for example) four columns is now four triples with the subject being repeated over and over, the predicate essentially being the column name, and the object being the column value. The SPARQL syntax offers these features: 1. Subjects and Objects can be used to find the other including transitively. Below is a set of triples. It should be clear that ex:sw001 and ex:sw002 link to ex:sw003, which itself has links:

In SPARQL, the first time a variable is encountered in the expression pipeline, it is populated with result. The second and subsequent times it is seen, it is used as an input. If we assign ("bind") the URI ex:sw003 to the ?targets variable, then it drives a result into ?src; this tells us all the things that link to ex:sw003 (upstream dependency):

But with a simple switch of the binding variable, the behavior is reversed. This will produce all the things upon which ex:sw003 depends (downstream dependency):

Even more attractive is that we can easily instruct SPARQL to transitively follow the path:

Bound variables can therefore also be lists and will be operated upon without complicated syntax. The effect of this is similar to the following pseudocode:

2. SPARQL expressions are a pipeline Unlike SQL which has subqueries and CTEs, SPARQL is much more like MongoDB or SPARK. Expressions are evaluated exactly in the order they are declared including filtering and joining of data. The programming model becomes what a SQL statement would be like with multiple WHERE clauses. The combination of list-aware subjects and objects plus a pipeline approach can yield extremely expressive queries spanning many different domains of data. JOIN as used in RDBMS and understanding the dynamics of the JOIN (e.g. what column in what table is suitable to join to another, inner vs. outer, etc.) is not relevant in SPARQL (and in some ways simpler) because objects, if a URI and not a literal, implicitly can be used only to find a subject. Here is a more comprehensive example that illustrates the pipeline using some syntax shortcuts.

Unlike relational databases, the object column is heterogeneous: the object data type, if not a URI, is usually implied (or specified in the ontology) by the predicate value. Literal nodes carry type information consistent with the underlying XSD namespace including signed and unsigned short and long integers, single and double precision floats, datetime, penny-precise decimal, Boolean, and string. Triple store implementations on traditional relational databases will typically store the value as a string and a fourth column will identify the real type. Polymorphic databases such as MongoDB and SQLite can store the native value directly into the object field. Thus, SPARQL provides a full set of analytic query operations such as JOIN, SORT, AGGREGATE for data whose schema is intrinsically part of the data rather than requiring a separate schema definition. However, schema information (the ontology) is often provided externally, to allow joining of different datasets unambiguously. In addition, SPARQL provides specific graph traversal syntax for data that can be thought of as a graph. The example below demonstrates a simple query that leverages the ontology definition foaf ("friend of a friend"). Specifically, the following query returns names and emails of every person in the dataset:

This query joins all of the triples with a matching subject, where the type predicate, "a", is a person (foaf:Person), and the person has one or more names (foaf:name) and mailboxes (foaf:mbox). For the sake of readability, the author of this query chose to reference the subject using the variable name "?person". Since the first element of the triple is always the subject, the author could have just as easily used any variable name, such as "?subj" or "?x". Whatever name is chosen, it must be the same on each line of the query to signify that the query engine is to join triples with the same subject. The result of the join is a set of rows – ?person, ?name, ?email. This query returns the ?name and ?email because ?person is often a complex URI rather than a human-friendly string. Note that any ?person may have multiple mailboxes, so in the returned set, a ?name row may appear multiple times, once for each mailbox, duplicating the ?name. An important consideration in SPARQL is that when lookup conditions are not met in the pipeline for terminal entities like ?email, then the whole row is excluded, unlike SQL where typically a null column is returned. The query above will return only those ?person where both at least one ?name and at least one ?email can be found. If a ?person had no email, they would be excluded. To align the output with that expected from an equivalent SQL query, the OPTIONAL keyword is required:

… excerpt ends here. Continue reading the full article.

Illustrations

SPARQL: The Wikidata Query Service can be used to query data from Wikidata using SPARQL[8][9]
The Wikidata Query Service can be used to query data from Wikidata using SPARQL[8][9]

Worked examples

Example 1 — a first encounter with SPARQL

Start with the simplest possible case. Write down what SPARQL claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to SPARQL before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about SPARQL ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of SPARQL

In research
SPARQL appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses SPARQL in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
SPARQL is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data modeling languages, Declarative programming languages, Programming languages created in 2008, so understanding it makes those chapters shorter.
In everyday life
Look for SPARQL outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study SPARQL in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what SPARQL means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain SPARQL out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is SPARQL in simple terms?

SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language—that is, a semantic query language for databases—able to retrieve and manipulate data stored in Resource Description Framework (RDF) format. It was made a standard by the RDF Data…

Why does SPARQL matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study SPARQL?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on SPARQL.

Tags

  • Data modeling languages
  • Declarative programming languages
  • Programming languages created in 2008
  • Query languages
  • RDF data access
  • Resource Description Framework
  • SPARQL
  • Web services
  • World Wide Web Consortium standards

Keep exploring