1 of 89

Query Wikidata using SPARQL like a Genius

Houcemeddine Turki / Csisc

Co-Founder / Wikidata Arabic Community

2 of 89

Wikidata

  • Launched in October 2012 by the Wikimedia Foundation.
  • Collaborative knowledge graph with structured data on people, places, species, organizations, and more.
  • Multilingual and openly accessible, supporting contributions in many languages.
  • Built on semantic web standards, enabling advanced queries via RDF and SPARQL.
  • Continuously updated by a global community and automated bots.

CC0, Planemad for the Wikimedia Foundation

​

​

3 of 89

SPARQL lets users query Wikidata to extract or integrate localized cultural, historical, or linguistic facts for preservation or reuse.

Storing local knowledge

Developers use SPARQL to power search, visualization, and recommendation tools that leverage structured data from Wikidata.

Building tools and applications

SPARQL queries help detect missing entries, contradictory statements, or incomplete data for quality assurance in Wikidata.

Identifying gaps and inconsistencies

4 of 89

5 of 89

  • Query Language for RDF Knowledge Graphs like Wikidata.
  • Allows to answer questions about the structured information available in Wikidata in real-time.
  • Adapted from SQL.

SPARQL

6 of 89

First

A Gentle Introduction to Wikidata for Absolute Beginners + Wikidata Query Tutorial

SPARQL – a gentle introduction to the Wikidata Query Service

First steps with SPARQL

Wikidata Query Service - the way forward for getting the most out of Wikimedia's knowledge graph

2017

2018

2023

2024

Previous Times

Second

Third

Fourth

User Interface SPARQL Basics

7 of 89

French

Advanced SPARQL

Vigneron

Italian

Epidosis

Swedish

Jan Ainali

Arabic

Csisc

Special Thanks to Tiago Lubiana and Adriano Rutz as well.

8 of 89

Time to show Advanced SPARQL in English

9 of 89

I will show how you can write a proper Wikidata code and debug it without using any feature of the Wikidata Query Service User Interface.

What we will do

10 of 89

https://query.wikidata.org

11 of 89

Query 1

“What does Wikidata know about Douglas Adams”

12 of 89

Principle 1: SPARQL Clauses

  • If you use SELECT *, all matching solutions are returned, including duplicates.
  • Add DISTINCT to remove repeated rows.

13 of 89

Principle 2: SPARQL Statements

  • As in Wikidata and other knowledge graphs, SPARQL statements are the form of triples:
    • Subject: Item represented by its Uniform Resource Identifier (URI).
    • Predicate: Property represented by its URI.
    • Object: Value (Constant) or an item (URI).
  • Statements are only defined after a WHERE clause
  • If we write the full URI of a resource, we put it between <>.
  • Variables begin with ?.

14 of 89

Principle 2: SPARQL Statements

The type of the object depends on the predicate.

15 of 89

Principle 2: SPARQL Statements

  • Sometimes, two successive statements can share the same subject.
  • Instead of repeating the subject twice, you can write the first statement followed by a semicolon (;) and then by the predicate and object of the second statement.

16 of 89

Trick 1: URI Prefixes

  • URI Prefixes are abbreviations that are used to shorten URIs when defined inside SPARQL statements.
  • Instead of writing full URIs, you can just write abbreviation:id.
  • There are plenty of predefined URI prefixes in the Wikidata Query Service. Each one of them is used to represent a certain part of a given statement: Item (wd:), Predicate (wdt:, p:), etc.

17 of 89

Example: Choose the right URI Prefixes to retrieve statement references

https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Full_list_of_prefixes

18 of 89

Trick 2: Autocomplete Shortcuts

  • Use Ctrl + Space (Windows/Linux) or Control + Space (Mac) to trigger autocomplete in the SPARQL editor.
  • If that does not work:
    • Try Ctrl + Alt + Space or Alt + Enter (Windows/Linux).
    • On macOS, try Control + Option + Space or Option + Enter.
    • On portable/wireless Mac keyboards, use Fn + Option + Return.

19 of 89

Solution: SELECT

20 of 89

Solution: FILTER

Task: Retrieve the English label of Douglas Adams (Q42).

SELECT ?itemLabel WHERE {

wd:Q42 rdfs:label ?itemLabel .

FILTER(LANG(?itemLabel) = "en")

}

Tip: Always filter by language for cleaner results.

Douglas Adams

PROPERTY

VALUE

rdfs:label

Label

schema:description

Description

skos:altLabel

Alias

dct:language

Language (Lexemes)

21 of 89

Operators

OPERATOR

VALUE

+

Addition

-

Subtraction

*

Multiplication

/

Division

&&

AND

!=

Different from

>

Greater than

OPERATOR

VALUE

()

Parentheses

=

Equal to

||

OR

!

NOT

>=

Superior or equal to

<=

Inferior or equal to

<

Lower than

22 of 89

Alternative 1: ASK

❓ Task: Is Douglas Adams (Q42) a human (Q5)?

ASK {

wd:Q42 wdt:P31 wd:Q5 .

}

💡 Tip: ASK returns true or false. Useful for boolean checks or validations.

23 of 89

Alternative 2: DESCRIBE

❓ Task: Give me everything known about Douglas Adams.

DESCRIBE wd:Q42

💡 Tip: DESCRIBE returns RDF triples about a resource. Used for exploration.

You can also use DESCRIBE with a SPARQL statement.

DESCRIBE ?president {?president wdt:P106 wd:Q11696}

24 of 89

Query 2

“Show a list of 1000 ancestors of Queen Elizabeth II”

25 of 89

P22

P25

26 of 89

Ancestors

Elizabeth II

father

mother

father

mother

Ancestor

27 of 89

Trick 3: Sorting and Pagination

Keyword

Purpose

Example Usage

Explanation

ORDER BY

Sorts the query results

ORDER BY DESC(?person)

Sorts the results in descending order (newest first). Use ASC() for ascending order.

LIMIT

Restricts the number of results returned

LIMIT 10

Only the first 10 results are shown.

OFFSET

Skips a number of results

OFFSET 10

Starts at the eleventh result. Use OFFSET 20 to skip the first 20, and so on for pagination.

28 of 89

Trick 4: Property Path Operators

Operator

Usage

Meaning

Example

+

?a wdt:P31+/wdt:P279* ?b

One or more occurrences of the predicate

Matches indirect instance chains

*

?a wdt:P279* ?b

Zero or more occurrences

Matches a class and any of its subclasses

^

?a ^wdt:P31 ?b

Inverse of the predicate

Traverses from object to subject

/

?a wdt:P31/wdt:P279 ?b

Sequence of properties

Follow a chain of two properties

29 of 89

Trick 4: Property Path Operators

Operator

Usage

Meaning

Example

!

?a !wdt:P31 ?b

Negation: match any property except this one

Excludes the specified predicate

()

?a (wdt:P31/wdt:P279)* ?b

Grouping

Groups property paths together

[ ]

Used in triple patterns: ?a ?p [ wdt:P31 wd:Q5 ]

If empty ([ ]): Any value

Anonymous node or property filter

Match with intermediate blank nodes or nested properties

|

?a (wdt:P31|wdt:P279) ?b

OR

Match on one of several properties

30 of 89

Solution: Query using Property Path Operators

31 of 89

Query 3

“Provide a list of Tunisian singers born after 1985”

32 of 89

Solution 1: Basic Query

Variable names are separated by spaces

33 of 89

Solution 2: Using VALUES for Multiple Occupations

34 of 89

Solution 3: Using FILTER on Date Directly

35 of 89

Trick 5: Defining constant values

"xyz"

string

0.0

float

-0

int

"x"

char

36 of 89

Solution 4: Using BIND to Extract the Year and HAVING to Filter

AS: Defines a variable name for an expression

37 of 89

Query 4

“Visualize cities in Tunisia on a map”

38 of 89

Solution: Geographical Coordinates

39 of 89

Solution: defaultView

🔍 Tip: #defaultView:Map at the top chooses how to display results.

40 of 89

Query 5

“Find twenty humans who do not have a known date of death”

41 of 89

Solution 1: Using FILTER NOT EXISTS

Filters out odd values from the results.

42 of 89

Solution 2: Using MINUS

Filters out odd values from the items meeting the previous condition.

43 of 89

Query 6

“Get fifty people under 18 or over 65.”

44 of 89

Solution 1: OR

45 of 89

Solution 2: UNION

46 of 89

Query 7

“Identify the gender of fifty people and assign UNKNOWN as a value if not available”

47 of 89

Solution 1: Optional, BOUND

48 of 89

Solution 2: Optional, COALESCE

49 of 89

Query 8

“Identify fifty English people having a first name that begins with A”

50 of 89

https://en.wikibooks.org/wiki/SPARQL/Expressions_and_Functions

51 of 89

Solution 1: REGEX

52 of 89

Solution 1: REGEX

https://w.wiki/EpJi

53 of 89

Solution 2: STRSTARTS

54 of 89

Solution 2: STRSTARTS

https://w.wiki/EpJj

55 of 89

Query 9

“Calculate the number of world heritage sites by country”

56 of 89

Trick 6: Aggregate Functions

Function

Description

Notes

COUNT(*)

Counts all matching results.

Use COUNT(?var) to count non-null values of a specific variable.

SUM(?var)

Calculates the sum of all numeric values.

Returns an error or NaN if applied to non-numeric values.

AVG(?var)

Computes the average of all numeric values.

Similar restrictions as SUM.

MIN(?var)

Finds the minimum value.

Works on numbers, strings, and other comparable types.

57 of 89

Trick 6: Aggregate Functions

Function

Description

Notes

MAX(?var)

Finds the maximum value.

Lexical order for strings; numeric order for numbers.

SAMPLE(?var)

Returns an arbitrary value from the group.

Useful when you expect one value or don’t care which value is returned.

GROUP_CONCAT(?var)

Concatenates all values into a single string (usually comma-separated).

Optional parameters allow setting custom separators, e.g., GROUP_CONCAT(?var; separator=", ").

58 of 89

Step 1: How to identify items

Use a known example

59 of 89

Step 2: Extract all heritage sites

60 of 89

Step 3: GROUP BY

What will be counted

Based on

61 of 89

Step 4: Define the aggregate function

62 of 89

Step 5: ORDER BY

63 of 89

Step 6: HAVING

64 of 89

Query 10

“Find all countries whose number of official languages is above the average number of official languages across all countries.”

65 of 89

Solution: Subqueries

𝟙. Compute the average number of official languages across all countries

𝟚. Reuse the result in the main query

𝟛. Calculate the number of official language for every country

𝟜. Filter out the countries having a number of official languages below the average

66 of 89

Step 1: Calculate the average

𝟙. Compute the number of official languages for every country

𝟚. Calculate the average number of official language across countries

67 of 89

Step 1.1: Number of Languages

68 of 89

Step 1.2: Calculate the average

Add a subquery inside the body of a SPARQL query:

  1. Put the subquery between two curly braces
  2. Insert it after the WHERE clause

69 of 89

Step 1.2: Calculate the average

Add a subquery inside the body of a SPARQL query:

  • Put the subquery between two curly braces
  • Insert it after the WHERE clause

70 of 89

Step 2: Reuse in the main query

Add a subquery inside the body of a SPARQL query:

  • Put the subquery between two curly braces
  • Insert it after the WHERE clause

71 of 89

Step 2: Reuse in the main query

Use WITH:

  • Add the subquery after a WITH clause
  • Add a name to the query using AS
  • Embed the subquery in the WHERE clause using INCLUDE

Not Standard

SPARQL

72 of 89

Step 3: Number of Languages

Same as 1.1

73 of 89

Step 4: Filter out countries below the average

74 of 89

Step 4: Filter out countries below the average

75 of 89

Query 11

“Find all medications that have significant drug interactions with the ones curing fever.”

76 of 89

Solution

https://w.wiki/EpK3

77 of 89

  • Do you know that the Wikidata Query Service optimizes your query before running it.
  • This means that the Wikidata Query Service reformulates your query before running it.

78 of 89

🔹 hint:Prior hint:runFirst true

This hint gives execution priority to a particular part of the query, usually a triple pattern or group.

  • runFirst true means: run this pattern first, before others.�
  • Often used when:�
    • You know a pattern will limit the result set drastically, helping performance.�
    • You want to override the optimizer’s default join order.

79 of 89

🔹 hint:Prior hint:runLast true

This is the opposite of runFirst.

  • It tells the engine to defer this pattern until the end.�
  • Can help when:�
    • The pattern is expensive or not selective.�
    • You want earlier parts of the query to reduce the data volume before running this.

80 of 89

Running the statement with the most constants last

https://w.wiki/EpKB

81 of 89

Running the statement with the most constants first

https://w.wiki/EpKD

82 of 89

Query 12

“Find all the scholarly publications of Nobel Prize winners in Chemistry from 2013 to 2022.”

83 of 89

query.wikidata.org

Main graph (all non-scholarly data)

query-scholarly.wikidata.org

Scholarly graph, includes scholarly articles and work

Why the split?�Wikidata’s growth (16B+ triples, +1B/year) made it too large for one query service (WDQS) to handle reliably, leading to slow queries, crashes, and long rebuild times.

​

🔄 Wikidata Graph Split (May 2025)

What changed?�As of 9 May 2025, the graph is split into two:

The scholarly graph endpoint has become an external SPARQL endpoint for the main SPARQL endpoint

84 of 89

Solution

SERVICE keyword is used to call external SPARQL endpoints or special internal services. This allows you to integrate data from multiple sources or use specific built-in services provided by Wikidata. Here's how it works:

85 of 89

SERVICE Type

Syntax

Purpose

Common Parameters

Example Use

Label Service

SERVICE wikibase:label

Fetch labels/descriptions in human languages

bd:serviceParam wikibase:language "en"

Show item labels instead of Q-IDs

MediaWiki API Service

SERVICE wikibase:mwapi

Access MediaWiki API (search, sitelinks, etc.)

wikibase:api, mwapi:search, mwapi:item, wikibase:apiOutputItem

Search entities by label

External SPARQL Service

SERVICE <https://external.sparql.endpoint>

Query external SPARQL endpoints (e.g., DBpedia)

None specific — write standard SPARQL inside

Combine data from Wikidata + DBpedia

Applications

86 of 89

Type

Example

Label Service

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

mwapi Search

SERVICE wikibase:mwapi { bd:serviceParam wikibase:api "EntitySearch"; mwapi:search "Paris"; mwapi:language "en". ?item wikibase:apiOutputItem mwapi:item. }

External

SERVICE <https://dbpedia.org/sparql> { ?s rdfs:label "Tunis"@en. }

Examples

87 of 89

Solution

https://w.wiki/EpKe

88 of 89

Jamal Nxedlana, from Bubblegum Club, on behalf of Moleskine Foundation, CC BY-SA 4.0

Time for work.

Spread the word.

89 of 89

Q&A