Skip to content
SDB
Knowledge Graphs

Chapter 04 · intermediate · 30 min

SPARQL — Querying Knowledge Graphs

The query language designed for RDF data

Subhendu Datta BhowmikAI Tutorials

What Is SPARQL?

SPARQL (SPARQL Protocol and RDF Query Language) is the standard query language for RDF knowledge graphs. It's to knowledge graphs what SQL is to relational databases.

SPARQL queries work by defining graph patterns — templates that are matched against the triples in the graph. Variables (prefixed with ?) are bound to matching values.

The Four Query Forms

FormReturnsUse case
SELECTTable of variable bindingsRetrieve facts
ASKBoolean (yes/no)Check if something exists
CONSTRUCTNew RDF graphTransform/extract subgraph
DESCRIBERDF description of a resourceExplore an unknown resource

Interactive query

Predicates: knows · worksAt · locatedIn

Find all peoplesparql
SELECT ?person WHERE {
  ?person rdf:type ex:Person .
}

Writing SPARQL Queries

Basic SELECT Query

Every SPARQL query has two key sections:

  • SELECT — which variables to return
  • WHERE — the graph pattern to match
Progressive SPARQL examples — from simple to complexsparql
PREFIX ex:   <http://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

# 1. Find all people
SELECT ?person WHERE {
  ?person rdf:type foaf:Person .
}

# ──────────────────────────────────────────
# 2. Find names of all people
SELECT ?person ?name WHERE {
  ?person rdf:type foaf:Person ;
          foaf:name ?name .
}

# ──────────────────────────────────────────
# 3. Find people over 30 (using FILTER)
SELECT ?name ?age WHERE {
  ?person foaf:name ?name ;
          foaf:age  ?age .
  FILTER (?age > 30)
}
ORDER BY DESC(?age)

# ──────────────────────────────────────────
# 4. Find people and their employer city
#    (multi-hop: person → company → city)
SELECT ?personName ?companyName ?cityName WHERE {
  ?person foaf:name     ?personName ;
          ex:worksAt    ?company .
  ?company foaf:name    ?companyName ;
           ex:locatedIn ?city .
  ?city    foaf:name    ?cityName .
}

# ──────────────────────────────────────────
# 5. OPTIONAL — include people even if no company known
SELECT ?name ?companyName WHERE {
  ?person foaf:name  ?name .
  OPTIONAL {
    ?person ex:worksAt ?company .
    ?company foaf:name ?companyName .
  }
}

CONSTRUCT and ASK

CONSTRUCT generates new RDF from the query result — useful for data transformation:

CONSTRUCT and ASK examplessparql
# CONSTRUCT — generate new triples from a query
CONSTRUCT {
  ?person <http://schema.org/worksFor> ?company .
}
WHERE {
  ?person ex:worksAt ?company .
}

# ──────────────────────────────────────────
# ASK — returns true/false
ASK {
  ex:alice ex:worksAt ex:acme .
}
# Returns: true

# ──────────────────────────────────────────
# UNION — match either pattern
SELECT ?entity WHERE {
  { ?entity rdf:type ex:Person . }
  UNION
  { ?entity rdf:type ex:Organization . }
}
Wikidata SPARQL example — find all Nobel Prize winners in Physicssparql
# Run this at: https://query.wikidata.org
SELECT ?person ?personLabel ?year WHERE {
  ?person wdt:P166 wd:Q38104 .          # awarded Nobel Prize in Physics
  ?person wdt:P569 ?birthDate .
  BIND(YEAR(?birthDate) AS ?birthYear)

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
ORDER BY ?year
LIMIT 20

Knowledge check

In SPARQL, what does the OPTIONAL keyword do?

Summary

In this chapter you learned:

  1. SPARQL uses graph patterns with variables (?x) to query RDF data
  2. The four query forms: SELECT (rows), ASK (boolean), CONSTRUCT (new graph), DESCRIBE (resource info)
  3. FILTER, OPTIONAL, and UNION add flexibility to patterns
  4. Public SPARQL endpoints like Wikidata let you explore real knowledge graphs

Next, we'll compare the RDF graph model to the Property Graph model used by Neo4j and TinkerPop.

Knowledge Graphs