Basic Queries
Master foundational SPARQL: SELECT, PREFIX, DISTINCT, FILTER, OPTIONAL, and property paths.
Start LearningMaster SPARQL through real-world queries. Explore knowledge graphs spanning programming languages, cultural heritage, and urban data using Wikidata and beyond.
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:
<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.
<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>
Now let's query this data. What if we want to ask: "Give me all the programming languages"?
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:
PREFIX example: <https://example.com/>
SELECT ?type {
example:C++ example:IsA ?type
}
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.
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:
SELECT ?proglang ?year {
?proglang wdt:P31 wd:Q9143;
wdt:P571 ?year.
}
Dive deep into SPARQL capabilities through progressive chapters, from foundational concepts to advanced federation techniques.
Master foundational SPARQL: SELECT, PREFIX, DISTINCT, FILTER, OPTIONAL, and property paths.
Start LearningWork with string manipulation, date functions, mathematical operations, and hash functions.
Explore FunctionsAnalyze data with COUNT, SUM, AVG, GROUP BY, HAVING, and GROUP_CONCAT for powerful insights.
Analyze DataGo beyond SELECT with ASK, CONSTRUCT, and DESCRIBE query forms for different use cases.
Level UpQuery multiple SPARQL endpoints simultaneously using SERVICE to combine knowledge from different sources.
Connect SourcesApply SPARQL to real-world domains. Explore specialized techniques for temporal, geospatial, and multilingual data.
Work with dates, timelines, and historical periods. Filter by time ranges and build chronological visualizations.
Explore TimeQuery locations, calculate distances, and visualize results on maps. Work with coordinates and regions.
Map the WorldQuery across 400+ languages. Handle labels, scripts, translations, and lexicographical data.
Go GlobalReusable patterns for common challenges: hierarchy traversal, deduplication, ranking, and optimization.
Master PatternsUse FILTER to narrow down results based on conditions:
SELECT ?proglang ?year {
?proglang wdt:P31 wd:Q9143;
wdt:P571 ?year.
FILTER (year(?year) > 2000)
}
Aggregate functions let you analyze your data:
SELECT (COUNT(?proglang) AS ?count) {
?proglang wdt:P31 wd:Q9143.
}
Use ASK to verify if data exists:
ASK {
?proglang wdt:P31 wd:Q9143;
wdt:P571 ?year.
}
Combine data from multiple sources using SERVICE:
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