Federation

SERVICE Keyword

Query remote SPARQL endpoints from within your query. The SERVICE keyword enables federated queries that combine data from multiple sources.

What is SERVICE?

The SERVICE keyword allows you to send part of your SPARQL query to a remote endpoint and integrate the results with your local query. This is the foundation of SPARQL federation — treating the entire Linked Data web as a queryable database.

Basic SERVICE Syntax

SERVICE Structure
SELECT ?item ?remoteData
WHERE {
  # Local query patterns
  ?item wdt:P31 wd:Q5 .

  # Remote query patterns
  SERVICE <http://dbpedia.org/sparql> {
    ?dbpediaItem owl:sameAs ?item .
    ?dbpediaItem dbo:abstract ?remoteData .
  }
}

The query engine executes the SERVICE block on the remote endpoint, then joins the results with the local patterns based on shared variables.

The Wikidata Label Service

The most common SERVICE call in Wikidata queries is the label service, which retrieves human-readable labels for entities.

Wikidata Label Service
Run ↗
SELECT ?country ?countryLabel ?capital ?capitalLabel
WHERE {
  ?country wdt:P31 wd:Q6256 .
  ?country wdt:P36 ?capital .

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en,fr,de" .
  }
}
LIMIT 20

The label service automatically creates ?variableLabel and ?variableDescription variables for each entity variable in your query.

Federated Query with DBpedia

Query external endpoints like DBpedia to enrich Wikidata results with additional information.

Enrich with DBpedia Data
Run ↗
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?lang ?langLabel ?homepage
WHERE {
  # Get programming languages from Wikidata
  ?lang wdt:P31 wd:Q9143 .

  # Get homepage from DBpedia
  SERVICE <http://dbpedia.org/sparql> {
    ?dbpediaLang owl:sameAs ?lang ;
                  foaf:homepage ?homepage .
  }

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
LIMIT 10

SERVICE SILENT

Use SERVICE SILENT to gracefully handle cases where the remote endpoint is unavailable. Without SILENT, a failed remote query causes the entire query to fail.

Graceful Failure Handling
Run ↗
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?lang ?langLabel ?homepage
WHERE {
  ?lang wdt:P31 wd:Q9143 .

  # SILENT: Don't fail if endpoint is unavailable
  SERVICE SILENT <http://dbpedia.org/sparql> {
    ?dbpediaLang owl:sameAs ?lang ;
                  foaf:homepage ?homepage .
  }

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
LIMIT 20

With SILENT, if the remote endpoint fails, the query continues with unbound variables for the remote data rather than throwing an error.

Variable Passing

Variables bound in the local query can be used in the SERVICE block to filter remote results. This is essential for joining data across endpoints.

Passing Variables to Remote Query
Run ↗
PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT ?author ?authorLabel ?abstract
WHERE {
  # Get authors born in France
  ?author wdt:P31 wd:Q5 ;
          wdt:P106 wd:Q36180 ;
          wdt:P19/wdt:P17 wd:Q142 .

  # Get their DBpedia abstract
  SERVICE <http://dbpedia.org/sparql> {
    ?dbAuthor owl:sameAs ?author ;
              dbo:abstract ?abstract .
    FILTER(LANG(?abstract) = "en")
  }

  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 5

Best Practices

Key Entities

Entity Description
wikibase:label Wikidata's built-in label service
bd:serviceParam Parameter configuration for Wikidata services
owl:sameAs Links equivalent entities across datasets
SERVICE SILENT Graceful failure handling for federated queries