An RDF Graph is traversed with the help of the SPARQL Query Language which forms Graph Patterns that will be matched by the RDF Graph.

In order to put some conditions on Graph Patterns one has to explicitly formulate Triples that express what can be matched and selected from the Graph.

In addition one can specify Constraints on results that one want to have selected. The Constraints can be given with the help of the FILTER keyword.

For example, given a SPARQL knowledge base with three Prefixes, dc, default and ns (namespace). dc stands for Dublin Core which is for identifying and characterizing bibliography. There are two books with title and prize.

If one wants to select from this database only books that costs less than a certain value one uses a FILTER expression. In the Query there one gives also the dc and ns namespaces, as well as selects the Title and the price of the books which is selected from the namespace that points to the book knowledge base. In the Graph Pattern one has the Variable x to select the book, but one selects only the books that have the property price lower than a certain value. For this books that fulfill the FILTER conditions one wants to select the titles.

  • The keyword FILTER specifies Constraints for the results
  • FILTER expressions contain Operators and Functions
  • FILTER can NOT assign/create new Values

A book Knowledge Base:

    # Default Graph (stored at
    @prefix dc: <>] .
    @prefix : <>] .
    @prefix ns: <>] .

    :book1 dc:title "SPARQL Tutorial" .
    :book2 ns:price 42.
    :book2 dc:title "The Semantic Web" .
    :book2 ns:price 23 .

A Query in SPARQL with the use of FILTER:

    PREFIX dc: <>]
    PREFIX ns: <>]
    SELECT ?title ?price
    FROM <>]
    WHERE {
         ?x ns:price ?price .
             FILTER (?price < 30.5)
         ?x dc:title ?title .
Edit tutorial

Comment on This Data Unit