Expressions
Learn string manipulation, date functions, and mathematical operations.
ContinueMaster the foundational building blocks of SPARQL. Learn to select, filter, and shape your queries to extract meaningful data from knowledge graphs.
This chapter covers the essential SPARQL concepts you need to start querying knowledge graphs effectively. Each topic builds on the previous one, taking you from simple selections to complex pattern matching.
All examples use Wikidata as the data source, so you can run them directly on the Wikidata Query Service to see real results.
Define namespace shortcuts to simplify your queries and improve readability.
BeginnerRetrieve specific variables from matched triple patterns in the knowledge graph.
BeginnerRemove duplicate results from your query output for cleaner data.
BeginnerRetrieve human-readable labels for entities in different languages.
BeginnerRestrict the number of results returned by your query.
BeginnerSkip a specified number of results for pagination.
BeginnerApply conditions to narrow down results based on specific criteria.
BeginnerInclude results even when optional patterns don't match.
IntermediateUse shorthand syntax with semicolons and commas for cleaner queries.
BeginnerAssign computed values to new variables within your query.
IntermediateProvide inline data as input to constrain query results.
IntermediateUse NOT EXISTS and FILTER NOT EXISTS to exclude patterns.
IntermediateSubtract matching patterns from your results.
IntermediateNavigate complex relationships with path expressions like *, +, and |.
IntermediateNest queries within queries for complex data retrieval.
IntermediateHere's a simple query demonstrating several basic concepts together:
# Find programming languages created after 2000
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?lang ?langLabel ?inception
WHERE {
?lang wdt:P31 wd:Q9143 . # instance of programming language
?lang wdt:P571 ?inception . # has inception date
FILTER(YEAR(?inception) > 2000)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY DESC(?inception)
LIMIT 10
This query selects entities where wdt:P31 (instance of) equals wd:Q9143
(programming language), and their inception dates (wdt:P571), filters to dates after 2000,
and returns unique results with English labels via wikibase:label, sorted by newest first and
capped at 10 rows.
After mastering basic queries, continue your journey with these chapters: