Open Source Learning Resource

SPARQLqueries

Master SPARQL through real-world queries. Explore knowledge graphs spanning programming languages, cultural heritage, and urban data using Wikidata and beyond.

50+
Queries
9
Chapters
3
Domains

What is SPARQL?

SPARQL is a powerful query language for exploring semantic web data in RDF format. RDF data consists of statements in the form of triples: subject-predicate-object. SPARQL queries leverage these triple patterns to extract meaningful insights from vast knowledge graphs.

Consider three programming languages stored in a database with unique identifiers:

RDF Identifiers
<https://example.com/C>
<https://example.com/C++>
<https://example.com/Python>

We use angle brackets to embed identifiers. With a relationship called IsA, we can express statements like "C is a Programming Language" that machines can understand and query.

RDF Triples
<https://example.com/C> <https://example.com/IsA> <https://example.com/ProgrammingLanguage>
<https://example.com/C++> <https://example.com/IsA> <https://example.com/ProgrammingLanguage>
<https://example.com/Python> <https://example.com/IsA> <https://example.com/ProgrammingLanguage>

Your First SPARQL Query

Now let's query this data. What if we want to ask: "Give me all the programming languages"?

Basic SELECT Query
SELECT ?proglang {
  ?proglang <https://example.com/IsA> <https://example.com/ProgrammingLanguage>
}

The variable ?proglang will match all entities that satisfy this pattern. Using the PREFIX keyword, we can simplify our queries:

Query with PREFIX
PREFIX example: <https://example.com/>

SELECT ?type {
  example:C++ example:IsA ?type
}

Real-World Data with Wikidata

To demonstrate SPARQL with real-life data, we use Wikidata β€” an open knowledge base containing information across countless domains. Wikidata provides a dedicated SPARQL endpoint where you can run queries and see live results.

Query Programming Languages on Wikidata
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>

SELECT ?proglang {
  ?proglang wdt:P31 wd:Q9143
}

Adding more triple patterns lets us retrieve additional properties. Here we get programming languages along with their inception dates:

Multiple Triple Patterns
SELECT ?proglang ?year {
  ?proglang wdt:P31 wd:Q9143;
            wdt:P571 ?year.
}

Explore Chapters

Dive deep into SPARQL capabilities through progressive chapters, from foundational concepts to advanced federation techniques.

πŸ“˜

Basic Queries

Master foundational SPARQL: SELECT, PREFIX, DISTINCT, FILTER, OPTIONAL, and property paths.

Beginner 15+ queries
Start Learning
πŸ”’

Expressions

Work with string manipulation, date functions, mathematical operations, and hash functions.

Intermediate String, Date, Math
Explore Functions
πŸ“Š

Aggregate Queries

Analyze data with COUNT, SUM, AVG, GROUP BY, HAVING, and GROUP_CONCAT for powerful insights.

Intermediate 7+ queries
Analyze Data
πŸš€

Advanced Forms

Go beyond SELECT with ASK, CONSTRUCT, and DESCRIBE query forms for different use cases.

Advanced ASK, CONSTRUCT, DESCRIBE
Level Up
🌐

Federation

Query multiple SPARQL endpoints simultaneously using SERVICE to combine knowledge from different sources.

Advanced Cross-endpoint
Connect Sources

Domain Chapters

Apply SPARQL to real-world domains. Explore specialized techniques for temporal, geospatial, and multilingual data.

⏰

Temporal Queries

Work with dates, timelines, and historical periods. Filter by time ranges and build chronological visualizations.

Intermediate Dates & Time
Explore Time
πŸ—ΊοΈ

Geospatial Queries

Query locations, calculate distances, and visualize results on maps. Work with coordinates and regions.

Intermediate Maps & Location
Map the World
🌍

Multilingual Queries

Query across 400+ languages. Handle labels, scripts, translations, and lexicographical data.

Intermediate Languages
Go Global
🧩

Query Patterns

Reusable patterns for common challenges: hierarchy traversal, deduplication, ranking, and optimization.

Advanced Best Practices
Master Patterns

Quick Examples

Filtering Results

Use FILTER to narrow down results based on conditions:

Programming Languages After 2000
Run β†—
SELECT ?proglang ?year {
  ?proglang wdt:P31 wd:Q9143;
            wdt:P571 ?year.
  FILTER (year(?year) > 2000)
}

Counting Results

Aggregate functions let you analyze your data:

Count Programming Languages
Run β†—
SELECT (COUNT(?proglang) AS ?count) {
  ?proglang wdt:P31 wd:Q9143.
}

Asking Questions

Use ASK to verify if data exists:

Check Data Existence
Run β†—
ASK {
  ?proglang wdt:P31 wd:Q9143;
            wdt:P571 ?year.
}

Federated Queries

Combine data from multiple sources using SERVICE:

Query Wikidata + DBpedia
SELECT ?proglang ?resource ?homepage {
  ?proglang wdt:P31 wd:Q9143.

  SERVICE <http://dbpedia.org/sparql> {
    ?resource rdf:type wd:Q9143;
              owl:sameAs ?proglang;
              foaf:homepage ?homepage.
  }
}
LIMIT 10