411 REFERENCE · Wikibooks
SPARQL/Wikidata Query Service
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Wikimedia runs the public service instance of WDQS (Wikidata Query Service), which is available for use at http://query.wikidata.org/.
GUI
The GUI at the home page of http://query.wikidata.org/ allows you to edit and submit SPARQL queries to the query engine. The results are displayed as an HTML table. Note that every query has a unique URL which can be bookmarked for later use. Going to this URL will put the query in the edit window, but will not run it - you still have to click "Execute" for that.
One can also generate a short URL for the query via a URL shortening service by clicking the "Generate short URL" link on the right - this will produce the shortened URL for the current query.
The "Add prefixes" button generates the header containing standard prefixes for SPARQL queries. The full list of prefixes that can be useful is listed in the RDF format documentation. Note that most common prefixes work automatically, since WDQS supports them out of the box.
The GUI also features a simple entity explorer which can be activated by clicking on the "🔍" symbol next to the entity result. Clicking on the entity Q-id itself will take you to the entity page on wikidata.org.
Default views
If you run the query in the WDQS GUI, you can choose which view to present by specifying a comment: #defaultView:viewName at the beginning of the query. Supported views are:
Table - default view, displays the results as a table of values
Map - displays coordinate points if any present in the result
ImageGrid - displays images present in the result as a grid
BubbleChart - displays bubble chart for numbers found in the result
TreeMap - displays hierarchical tree map for numbers found in the result
Timeline - for results having dates, displays timeline placing each row at appropriate time
Dimensions - displays rows as lines between points on the scales representing each column
Graph - displays result as a connected graph, using linkTo column
SPARQL endpoint
SPARQL queries can be submitted directly to the SPARQL endpoint with a GET request to https://query.wikidata.org/sparql?query=SPARQL POST requests can be sent to query.wikidata.org/bigdata/namespace/wdq/sparql. The result is returned as XML by default, or as JSON if either the query parameter format=json or the header Accept: application/sparql-results+json are provided.
JSON format is standard SPARQL 1.1 Query Results JSON Format.
Supported formats
The following output formats are currently supported by the SPARQL endpoint:
FormatHTTP HeaderQuery parameterDescriptionXMLAccept: application/sparql-results+xmlformat=xmlXML result format, is returned by default. As specified in https://www.w3.org/TR/rdf-sparql-XMLres/JSONAccept: application/sparql-results+jsonformat=jsonJSON result format, as in: https://www.w3.org/TR/sparql11-results-json/TSVAccept: text/tab-separated-valuesAs specified in https://www.w3.org/TR/sparql11-results-csv-tsv/CSVAccept: text/csvAs specified in https://www.w3.org/TR/sparql11-results-csv-tsv/Binary RDFAccept: application/x-binary-rdf-results-table
Query timeout
There is a hard query deadline configured which is set to 60 seconds. That is true both for the GUI and the public SPARQL endpoint. If your query does not execute in the allowed time, try to optimize it, or reduce the amount of data it returns.
Every query will timeout when it takes more time to execute than this configured deadline. You may want to optimize the query or report a problematic query here mw:Wikidata_query_service/Problematic_queries
Introduction to Wikidata Query Service
See SPARQL/Wikidata Query Service - Introduction
References
Wikidata query service/User Manual
Other SPARQL Endpoints
See www.w3.org/wiki/SparqlEndpoints for a list of other SPARQL Endpoints.