A back arrow icon.
RDFox Blog

SPARQL Basics and RDFox

SPARQL Basics and RDFox
Felicity Mulford

This article aims to provide a basic introduction to SPARQL with RDFox. It will explain what SPARQL is, why RDFox uses SPARQL, how to write SPARQL queries, how to monitor SPARQL queries with RDFox, and other SPARQL related resources.

What is SPARQL?

SPARQL originally stood for ‘Simple Protocol and RDF Query Language’. However, SPARQL turned out not to be so simple... so the acyronym was changed and the new recursive definition is ‘SPARQL Protocol and RDF Query Language’.

SPARQL is designed and maintained by the W3C and is the standard query language for RDF graph databases. SPARQL provides a similar function to SQL for relational databases, but for graph databases, by enabling information within the RDF graph to be queried.

What is a graph database?

What is RDFox?

RDFox is an in-memory, high-performance, knowledge graph and semantic reasoning engine. Given by the name, at its core RDFox is an RDF graph database. It is optimised for speed and reasoning, which are unmatched in power. RDFox supports flexible, incremental addition and retraction of data, and incremental reasoning. It was designed and mathematically validated at the University of Oxford.

What is a knowledge graph?

What is a semantic reasoning engine?

Why does RDFox use SPARQL?

SPARQL is the standard query language for RDF graph databases. By using a standard language, RDFox users are not being locked into a proprietary language, allowing more flexibility for the user. Additionally, the large amount of documentation and support on the language means it is widely accessible by users and prevents barriers to RDFox’s application.

RDFox supports most SPARQL 1.1 query language features, with the exceptions listed here, as well as the most commonly needed parts of the SPARQL 1.1 update. RDFox supports INSERT and DELETE query operations, which can be used to remove/add triples from/to the Knowledge Graph Store. RDFox also implements a few proprietary built-in functions that are not in SPARQL 1.1.

RDFox supports all four output formats for SPARQL results: XML, JSON, CSV and TSV.

How to write SPARQL queries with RDFox

A SPARQL query is a request for data or information from a knowledge graph. Queries retrieve and model data stored within RDFox. SPARQL queries can also contain subqueries and filters which enable the user to make their query more specific, ultimately improving their ability to obtain information from the RDF graph database.

You can interact with RDFox through the command line interface, referred to as the shell, or through a web interface, named the RDFox console. The shell can also be accessed through an integrated development environment (IDE) for example, Visual Studio Code.

This article uses the Family Guy data from the Getting Started Guide, Webinar and documentation to illustrate the queries provided. This data is as follows:

@prefix : <https://oxfordsemantic.tech/RDFox/getting-started/> .

:peter :forename "Peter" ;
   a :Person ;
   :marriedTo :lois ;
   :gender "male" .

:lois :forename "Lois" ;
   a :Person ;
   :gender "female" .

:meg :forename "Meg" ;
   a :Person ;
   :hasParent :lois, :peter ;
   :gender "female" .

:chris :forename "Chris" ;
   a :Person ;
   :hasParent :peter ;
   :gender "male" .

:stewie :forename "Stewie" ;
   a :Person ;
   :hasParent :lois ;
   :gender "male" .

:brian :forename "Brian" . # Brian is a dog

Queries can be typed or pasted directly into the shell or in the RDFox console. The RDFox console is a user interface which was launched with Version 3 and has seen many improvements since! It is optimised for query writing and provides graph visualisation. At present, the RDFox console can be used for SELECT queries only, whilst the shell can be used for all query types outlined in this article.

The following images show firstly, the RDFox console with a SELECT query which is requesting RDFox to return all triples in the graph database. Secondly, graph visualisation of the query result, returning all triples from the Family Guy data store. Data found in the Getting Started Guide, Webinar and documentation:

RDFox console running a select query on the Family Guy data store, asking to return all triples
The Graph Visualisation of the result of the SELECT query on the Family Guy Data store, returning all triples

There are different types of SPARQL queries, for example: SELECT, INSERT, DELETE, CONSTRUCT and ASK.

SELECT, CONSTRUCT and ASK queries do not modify the graph database, they only return the information to the user, whilst INSERT and DELETE queries modify the graph database.

The following examples use the same Family Guy character data store used in the Getting Started Guide, Webinar and documentation.

The SELECT Query

SELECT queries select all data which matches the WHERE part of the query. For example, this SELECT query will bring back the information on each person’s forename in the data store.

SELECT ?p ?n WHERE { ?p rdf:type :Person . ?p :forename ?n }

This returns the following answers:

:meg “Meg” .:stewie “Stewie” .
:chris “Chris” .:lois "Lois" .:Peter "Peter" .

Note Brian is not returned as he is not given the type :Person, as he is a dog.

The CONSTRUCT Query

Similarly to a SELECT query, the CONSTRUCT query finds all data which matches the WHERE part of query. However, rather than merely returning the information like in a SELECT query, at the point where the match is found, new triples are constructed. However, these are not added to the graph database as new triples.

For example, in the following query, if the child has a parent, and that parent has a sibling, then that parent’s sibling, depending on their gender, will be determined by RDFox to be the childs aunt or uncle.

For uncle, the construct query would be:

CONSTRUCT {?child :hasUncle ?sibling} WHERE {
   ?child :hasParent ?parent .
   ?parent :hasSibling ?sibling .
   ?sibling :gender "male".
}

And for aunt it would be the following:

CONSTRUCT {?child :hasAunt ?sibling} WHERE {
   ?child :hasParent ?parent .
   ?parent :hasSibling ?sibling .
   ?sibling :gender "female".
}

ASK

The ASK query, asks whether or not a pattern is found within the data store and prints out the total number of matched solutions.

For example, the following query asks if lois has the child stewie.

ASK { :lois :hasChild :Stewie }

This ASK query will show that the pattern has been matched, as the triple which states that Lois has a child called Stewie is within our data store.

If we ran the query:

ASK { :lois :hasChild :Brian }

There would be no match, because the pattern is not stored within the data. Lois does not have a child called Brian, instead Brian is their dog.

The INSERT Update

The INSERT query adds facts to the data store. For example, we can modify the data store to make the married statement symmetric. The original data store contained a triple stating that Peter is married to Lois, but not that Lois was married to Peter. This query will then insert a triple stating that Lois is also married to Peter.

INSERT { ?x :marriedTo ?y } WHERE { ?y :marriedTo ?x }

Now both of the following triples are stored in the data store:

:peter :married_to :lois .
:lois :married_to :peter .

DELETE

A DELETE query can be used to remove facts from the data store. For example, we can delete the triple that says Stewie has the parent Lois from the data store with the following:

DELETE { :stewie :hasParent :lois } WHERE { :stewie :hasParent :lois }

For tips and best practise on writing queries see our article. To watch queries in action, check out our Getting Started Webinar which runs through step-by-step how to implement SELECT, INSERT and DELETE queries using the Family Guy dataset.

RDFox SPARQL built-in functions:

Built-in functions specific to RDFox, expand the functions available to RDFox users. These include:

  • SKOLEM
  • DATETIME
  • TIME_ON_TIMELINE
  • IFERROR
  • MINFN
  • MAXFN

For more information on what these built-in functions do, read the documentation.

Monitoring SPARQL query execution with RDFox:

RDFox allows the user to monitor the execution of queries and provides useful statistics about the execution of queries, through access to query plans. Suppose that we initialize a data store with the example data in our Getting Started guide. The following shell command provides access to the query plans produced by RDFox:

set query.explain true

Now, let’s issue the following SPARQL query against the store:

SELECT ?p ?n WHERE {
   ?p rdf:type :Person .
   ?p :forename ?n .
   ?p :hasParent ?z .
}

Which returns the following answers:

:meg “Meg” .
:stewie “Stewie” .
:chris “Chris” .
:meg “Meg” .

The shell now also displays the query plan that has been actually executed:

SPARQL Query Plan shown in RDFox

The query plan is executed top-down in a depth-first-search manner and we can think of solution variable bindings as being generated one-at-a-time. It is useful to go in more detail through the execution of the plan for a given solution binding.

For more complex queries, including using the OPTIONAL operator in SPARQL read the documentation.

SPARQL and reasoning (Datalog)

Although RDFox can execute SPARQL queries in record speed, uncontested by other graph databases, the most accomplished users of RDFox also use rules written in Datalog. Rules allow for the materialisation of facts prior to query-time — making SPARQL queries in RDFox even faster! To learn how to write rules in Datalog, read our tips and tricks article.

SPARQL Resources:

There are numerous resources online to help you with writing and understanding when to use SPARQL queries. For example, the W3 provide information on SPARQL endpoints, common SPARQL extensions, the SPARQL specification and a working group on SPARQL.

SPARQL Tutorials

If you, or anyone within your organisation, are interested in participating in a SPARQL tutorial with Oxford University Professors and Oxford Semantic Technologies’ founders, email info@oxfordsemantic.tech.

We regularly run open classes for free so check out our events page to see any that are coming up!

Try writing SPARQL queries yourself

To try writing SPARQL queries, you can request an RDFox license here. You can learn more about RDFox here or on our blog.

Take your first steps towards a solution.

Start with a free RDFox demo!

Take your first steps towards a solution.

Get started with RDFox for free!

Team and Resources

The team behind Oxford Semantic Technologies started working on RDFox in 2011 at the Computer Science Department of the University of Oxford with the conviction that flexible and high-performance reasoning was a possibility for data-intensive applications without jeopardising the correctness of the results. RDFox is the first market-ready knowledge graph designed from the ground up with reasoning in mind. Oxford Semantic Technologies is a spin-out of the University of Oxford and is backed by leading investors including Samsung Venture Investment Corporation (SVIC), Oxford Sciences Enterprises (OSE) and Oxford University Innovation (OUI).