SPARQL

What is SPARQL?

SPARQL is a SQL-like query language for RDF data. SPARQL queries can produce result sets that are tabular or RDF graphs depending on the kind of query used.

  • SELECT is similar to the SQL SELECT in that it produces tabular result sets.
  • CONSTRUCT creates a new RDF graph based on query results.
  • ASK returns Yes or No depending on whether the query has a solution.
  • DESCRIBE returns the RDF graph data about a resource. This is, of course, useful when the query client doesn’t know the structure of the RDF data in the data source.
  • INSERT adds triples to a graph,
  • DELETE removes triples from a graph.

Let’s use SPARQL, the query language for RDF graphs, to create a graph. To write the SPARQL query which creates an RDF graph, perform these steps:

First, define prefixes to URIs with the PREFIX keyword. In the example below, we set bedrock as the default namespace for the query.

Next, use INSERT DATA to signify you want to insert statements. Write the subject predicate object statements.

Finally, execute this query:

_images/sparql-insert-flintstones.png

As you can see in the example shown in the gray box, we wrote a query which included PREFIX, INSERT DATA, and several subject predicate object statements, which are:

Fred has spouse Wilma, Fred has child Pebbles, Wilma has child Pebbles, Pebbles has spouse Bamm-Bamm, and Pebbles has children Roxy and Chip.

Now, let’s write a SPARQL query to access the RDF graph you just created.

First, define prefixes to URIs with the PREFIX keyword. As in the earlier example, we set bedrock as the default namespace for the query.

Next, use SELECT to signify you want to select certain information, and WHERE to signify your conditions, restrictions, and filters.

Finally, execute this query:

_images/sparql-select-flintstones.png

As you can see in this example shown in the gray box, we wrote a SPARQL query which included PREFIX, SELECT, and WHERE. The red box displays the information which is returned in response to the written query. We can see the familial relationships between Fred, Pebbles, Wilma, Roxy, and Chip.

SPARQL is quite similar to SQL, however, unlike SQL which requires SQL schema and data in SQL tables, SPARQL can be used on graphs and does not need a schema to be defined initially.

In the following example, we will use SPARQL to find out if Fred has any grandchildren.

First, define prefixes to URIs with the PREFIX keyword.

Next, we use ASK to discover whether Fred has a grandchild, and WHERE to signify the conditions.

_images/sparql-ask-flintstones.png

As you can see in the query in the green box, Fred’s children’s children are his grandchildren. Thus the query is easily written in SPARQL by matching Fred’s children and then matching his children’s children. The ASK query returns “Yes” so we know Fred has grandchildren.

If instead we want a list of Fred’s grandchildren we can change the ASK query to a SELECT one:

_images/sparql-select2-flintstones.png

The query results, reflected in the red box, tell us that Fred’s grandchildren are Roxy and Chip.

Using SPARQL in GraphDB

The easiest way to execute SPARQL queries in GraphDB is by using the GraphDB Workbench. Just choose SPARQL from the navigation bar, enter your query and hit Run, as shown in this example:

_images/sparql.png

See also

You can also watch the video from GraphDB Fundamentals Module 2: