Skip to content
    All resources

    Your first graph query

    Connect three example pages to their topics, then retrieve matching titles and source addresses with SPARQL.

    1. Model a small collection

    This example describes three fictional web pages. Each page has a title, a topic and a source address. Two pages share the KnowledgeGraphs topic. Their shared identifier makes the relationship explicit even though their titles use different words.

    You need an RDF store that accepts Turtle and a SPARQL query interface. Use a separate example dataset. The example.org addresses below identify fictional data; they are not live documentation links.

    2. Load the example data

    Download the Turtle file and import it into the default graph of your example dataset. Turtle expresses statements as subject, predicate and object. Here, ex:page1 is a subject, ex:about is a predicate and ex:KnowledgeGraphs is an object.

    The ex: prefix abbreviates https://example.org/. The semicolon continues with another predicate for the same subject. These predicates form a small example vocabulary; a production model should choose terms and constraints appropriate to its domain.

    Turtle
    @prefix ex: <https://example.org/> .
    
    ex:page1 ex:title "A guide to knowledge graphs" ;
        ex:about ex:KnowledgeGraphs ;
        ex:url <https://example.org/guides/graphs> .
    
    ex:page2 ex:title "Connecting sources with RDF" ;
        ex:about ex:KnowledgeGraphs ;
        ex:url <https://example.org/guides/rdf> .
    
    ex:page3 ex:title "An introduction to search" ;
        ex:about ex:Search ;
        ex:url <https://example.org/guides/search> .
    

    3. Ask for pages about knowledge graphs

    Run this query against the default graph containing the sample. The WHERE pattern matches pages with the required topic, title and source address. SELECT chooses the columns returned, and ORDER BY makes their order predictable.

    SPARQL
    PREFIX ex: <https://example.org/>
    
    SELECT ?title ?source
    WHERE {
      ?page ex:about ex:KnowledgeGraphs ;
            ex:title ?title ;
            ex:url ?source .
    }
    ORDER BY ?title
    

    4. Check the result

    Expect two rows: “A guide to knowledge graphs” with https://example.org/guides/graphs, followed by “Connecting sources with RDF” with https://example.org/guides/rdf. The page about search does not match the chosen topic.

    Try replacing ex:KnowledgeGraphs in the query with ex:Search. You should get one row: “An introduction to search”. If you get no results, check that the data was imported into the default graph and that the namespace matches exactly.

    This query follows relationships explicitly stored in the data. It does not infer missing facts or assess whether a source is trustworthy. For real data, retain source versions and distinguish assertions from derived conclusions.