SENG-42273 · Semantic Web & Ontology

Learn SPARQL

The W3C standard query language for retrieving and manipulating data stored in RDF format. Master RDF querying with hands-on examples.

10 Lessons 4 Hours Beginner Friendly W3C Standard

What is SPARQL?

SPARQL (SPARQL Protocol and RDF Query Language) is the standard query language for retrieving and manipulating data stored in RDF format. Developed by the W3C, it's the cornerstone of the Semantic Web.

Think of SPARQL as SQL for linked data — but instead of querying tables, you query graphs of interconnected information.

Understanding RDF Triples

RDF data is stored as triples: Subject → Predicate → Object. This forms the fundamental building block of all SPARQL queries.

<http://example.com/John> <http://example.com/hasCar> <http://example.com/Car1> ↑ Subject ↑ Predicate ↑ Object "John has a car named Car1"
Subject

The resource being described

Predicate

The property or relationship

Object

The value or related resource

Part I — SPARQL Fundamentals

Core syntax, SELECT queries, and pattern matching

Query Syntax Basics
Structure and Components
01
  • PREFIX declarations
  • Query structure overview
  • Variable naming (?var)
  • URI syntax and shortcuts
SELECT Statement
Retrieving Data
02
  • Basic SELECT syntax
  • Selecting specific variables
  • SELECT * vs SELECT ?var
  • Aliasing with AS
WHERE Clause
Pattern Matching
03
  • Triple patterns
  • Matching graph data
  • Multiple patterns
  • Pattern syntax rules
FILTER Clause
Refining Results
04
  • Comparison operators
  • REGEX pattern matching
  • Logical operators (&&, ||)
  • Built-in functions

Part II — Intermediate Techniques

OPTIONAL, UNION, and result modifiers

OPTIONAL Patterns
Handling Missing Data
05
  • OPTIONAL syntax
  • Partial matches
  • Unbound variables
  • OPTIONAL vs required
UNION Queries
Combining Patterns
06
  • UNION syntax
  • Alternative patterns
  • UNION vs OPTIONAL
  • Multiple UNIONs
Result Modifiers
Sorting & Pagination
07
  • ORDER BY (ASC/DESC)
  • LIMIT and OFFSET
  • DISTINCT keyword
  • Pagination patterns

Part III — Advanced Topics

Aggregations, endpoints, and best practices

Aggregate Functions
COUNT, SUM, AVG
08
  • COUNT, SUM, AVG, MIN, MAX
  • GROUP BY clause
  • HAVING filter
  • Aggregate expressions
SPARQL Endpoints
Public Data Sources
09
  • What are endpoints?
  • DBpedia, Wikidata
  • HTTP protocol
  • Result formats
Best Practices
Tips & Optimization
10
  • Query optimization
  • Common mistakes
  • Performance tips
  • Real-world examples
Recommended Learning Path
Week 1–2Master SELECT, WHERE, and basic triple patterns. Practice with simple queries.
Week 3–4Learn FILTER, OPTIONAL, and UNION. Handle complex data scenarios.
Week 5–6Explore aggregations, endpoints, and real-world datasets like DBpedia.

SPARQL Tutorials

SELECT Statement

The SELECT statement retrieves data from an RDF dataset. Variables (starting with ?) represent the data you want to return.

PREFIX ex: <http://example.com/> SELECT ?person ?car WHERE { ?person ex:hasCar ?car . }

Key Points

  • PREFIX defines namespace shortcuts
  • SELECT lists variables to return
  • WHERE contains triple patterns to match
  • Each triple pattern ends with a period (.)

Try It

?person?car
ex:Johnex:Tesla_Model3
ex:Maryex:BMW_X5
ex:Bobex:Honda_Civic

FILTER Clause

The FILTER clause allows you to specify conditions that results must meet. Use comparison operators, regex, and logical operators.

PREFIX ex: <http://example.com/> SELECT ?car ?price WHERE { ?car ex:hasPrice ?price . FILTER (?price > 30000) }
Equality

=, !=

Comparison

<, >, <=, >=

Logical

&&, ||, !

REGEX

Pattern matching

-- String pattern matching FILTER (REGEX(?name, "John", "i")) -- Multiple conditions FILTER (?price > 1000 && ?price < 5000) -- Check if bound FILTER (BOUND(?email))

Try It

?car?price
ex:Tesla_Model345000
ex:BMW_X565000
ex:Mercedes_C30042000

OPTIONAL Clause

OPTIONAL patterns are matched if possible. If they don't match, the query still returns results with unbound variables.

SELECT ?person ?car ?color WHERE { ?person ex:hasCar ?car . OPTIONAL { ?car ex:hasColor ?color . } }

When to Use OPTIONAL

  • When data might be missing for some results
  • To include additional info without excluding results
  • For enriching results with supplementary data

Try It

?person?car?color
ex:Johnex:TeslaRed
ex:Maryex:BMW(unbound)
ex:Bobex:HondaBlue

UNION Clause

UNION combines results from multiple patterns. It's like an OR operation — matches either pattern.

SELECT ?person ?vehicle WHERE { { ?person ex:hasCar ?vehicle . } UNION { ?person ex:hasBike ?vehicle . } }
UNION vs OPTIONAL
UNIONOPTIONAL
Match A OR BMatch A, and B if possible
Alternative pathsEnhancement of results
Separate result setsExtended existing results

Try It

?person?vehicle
ex:Johnex:Tesla (car)
ex:Johnex:Trek (bike)
ex:Maryex:BMW (car)
ex:Bobex:Schwinn (bike)

Result Modifiers

Control how results are sorted, limited, and paginated using ORDER BY, LIMIT, OFFSET, and DISTINCT.

ORDER BY

Sort results ASC/DESC

LIMIT

Max results to return

OFFSET

Skip N results

DISTINCT

Remove duplicates

PREFIX ex: <http://example.com/> SELECT DISTINCT ?person ?age WHERE { ?person ex:hasAge ?age . } ORDER BY DESC(?age) LIMIT 10 OFFSET 5
Pagination Pattern
PageLIMITOFFSET
Page 1100
Page 21010
Page 31020
Page N10(N-1) × 10

Try It

?person?age
ex:Alice65
ex:Bob52
ex:Carol45
ex:David38
ex:Eve29

Aggregate Functions

SPARQL supports aggregation functions like COUNT, SUM, AVG, MIN, and MAX.

FunctionPurposeExample
COUNT(?x)Count resultsTotal cars
SUM(?x)Sum valuesTotal price
AVG(?x)AverageAvg price
MIN(?x)MinimumCheapest
MAX(?x)MaximumMost expensive
SELECT ?brand (COUNT(?car) AS ?count) (AVG(?price) AS ?avgPrice) WHERE { ?car ex:hasBrand ?brand . ?car ex:hasPrice ?price . } GROUP BY ?brand HAVING (COUNT(?car) > 2)

Try It

?brand?count?avgPrice
Tesla352000
BMW258000
Honda528000

SPARQL Endpoints

A SPARQL endpoint is a web service that provides access to an RDF dataset. You send queries via HTTP and receive results in JSON, XML, or CSV.

DBpedia

Wikipedia structured data

https://dbpedia.org/sparql
Wikidata

Wikimedia knowledge base

https://query.wikidata.org/
UniProt

Protein database

https://sparql.uniprot.org/
LinkedGeoData

OpenStreetMap data

http://linkedgeodata.org/sparql
PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX dbr: <http://dbpedia.org/resource/> SELECT ?city ?population WHERE { ?city rdf:type dbo:City . ?city dbo:country dbr:United_States . ?city dbo:populationTotal ?population . } ORDER BY DESC(?population) LIMIT 10

HTTP Access

  • Send queries via HTTP GET or POST
  • Results available in JSON, XML, CSV formats
  • Use Accept header to specify format
  • URL-encode your query parameter

Try It

?city?population
dbr:New_York_City8336817
dbr:Los_Angeles3979576
dbr:Chicago2693976
dbr:Houston2320268
dbr:Phoenix,_Arizona1680992

Best Practices & Optimization

Write efficient, readable, and maintainable SPARQL queries with these proven techniques.

Performance Tips
TipWhy It Helps
LIMIT during developmentPrevents timeout on large datasets
Use rdf:type earlyNarrows search space quickly
Be specific with patternsReduces matching candidates
Avoid SELECT *Returns only needed data
Put restrictive filters earlyEliminates rows sooner

Slow Query

  • SELECT *
  • WHERE { ?s ?p ?o }
  • No type restriction
  • Returns everything!

Fast Query

  • SELECT ?name
  • ?x rdf:type ex:Person
  • Specific variables
  • Limited scope

Query Organization

Structure your queries logically for readability and maintainability:

# 1. PREFIX declarations at the top PREFIX ex: <http://example.com/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> # 2. SELECT with only needed variables SELECT ?name ?age ?email WHERE { # 3. Most restrictive patterns first ?person rdf:type ex:Employee . ?person ex:department ex:Engineering . # 4. Required patterns ?person ex:name ?name . ?person ex:age ?age . # 5. FILTER after related patterns FILTER (?age >= 25) # 6. OPTIONAL patterns last OPTIONAL { ?person ex:email ?email } } # 7. Modifiers at the end ORDER BY ?name LIMIT 100

Common Mistakes to Avoid

  • Missing period (.) at end of triple patterns
  • Case sensitivity issues (URIs are case-sensitive!)
  • Forgetting PREFIX declarations
  • Not using LIMIT on large/unknown datasets
  • Confusing OPTIONAL with UNION
  • Using = instead of pattern matching

Debugging Tips

  • Start simple, add complexity gradually
  • Test each pattern addition before moving on
  • Use LIMIT 1 to verify patterns match
  • Check variable spelling (case matters)
  • Verify URIs exist in the dataset

Optimized Query Example

?name?age?email
Alice Smith32[email protected]
Bob Johnson28[email protected]
Carol White45(unbound)
David Brown38[email protected]

SPARQL Cheat Sheet

Keywords Reference
KeywordPurposeExample
PREFIXDefine namespace shortcutsPREFIX ex: <…>
SELECTSpecify variables to returnSELECT ?name ?age
WHEREDefine triple patternsWHERE { ?s ?p ?o }
FILTERAdd conditionsFILTER (?age > 18)
OPTIONALMatch if possibleOPTIONAL { ?x ex:e ?e }
UNIONCombine alternatives{ … } UNION { … }
ORDER BYSort resultsORDER BY DESC(?p)
GROUP BYGroup for aggregationGROUP BY ?brand
HAVINGFilter groupsHAVING (COUNT(?x) > 5)
LIMITLimit result countLIMIT 10
OFFSETSkip resultsOFFSET 20
DISTINCTRemove duplicatesSELECT DISTINCT ?n
Functions Reference
FunctionTypeDescription
COUNT(?x)AggregateCount number of values
SUM(?x)AggregateSum of numeric values
AVG(?x)AggregateAverage of values
MIN(?x)AggregateMinimum value
MAX(?x)AggregateMaximum value
REGEX(?s, p)StringPattern matching
STR(?x)StringConvert to string
LANG(?x)StringGet language tag
BOUND(?x)TestCheck if variable bound
isURI(?x)TestCheck if value is URI

Common Mistakes

  • Missing period at end of triple patterns
  • Case sensitivity issues (URIs are case-sensitive)
  • Forgetting PREFIX declarations
  • Not using LIMIT on large datasets
  • Confusing OPTIONAL with UNION

Best Practices

  • Always use LIMIT during development
  • Be specific — more patterns = faster queries
  • Use rdf:type early to narrow results
  • Only SELECT variables you need
  • Put restrictive filters early

SPARQL Playground

Interactive Query Editor

Try Real Endpoints

Practice with actual SPARQL endpoints to query real-world data:

DBpedia SPARQL

Query Wikipedia's structured data

Try it live →
Wikidata Query

Explore the knowledge base

Try it live →