Shortcut: Wikidata:Gentle Query

Wikidata:SPARQL query service/A gentle introduction to the Wikidata Query Service

From Wikidata
Jump to navigation Jump to search

A gentle introduction to the Wikidata Query Service[edit]

Have you ever wondered about questions like these?

  • What are the largest cities in the world that have a female mayor?
  • What airports are located within 100km of Berlin?
  • Who were all known descendants of Genghis Khan?

Wikidata has the answers.

What is Wikidata?[edit]

Wikidata is a repository of knowledge. You can read an introduction to the idea and concepts behind Wikidata on our introduction page.

How do I ask a question?[edit]

The query language used to ask questions like that on Wikidata is called SPARQL. If this sounds too technical and overly complicated for you, don't worry. For this gentle introduction you won't have to learn SPARQL. Instead, we will look at existing example queries and how to edit them.

What is a query?[edit]

A query is a special form of question that computer systems can understand and answer. Unless you are interacting with an Artificial Intelligence, you would need to learn how to formulate your questions in a certain way.

Can I freely use this data?[edit]

Oh yes. Wikidata can be used by anyone for everything because all data is in the Public Domain (CC0).

What now?[edit]

We'll start with a simple example to show you how to use this service, and as we build up to more elaborate examples, you’ll get the answer to the fundamental question. Promise.

How to use Wikidata for a simple query[edit]

Let’s go through a simple example demonstrating how to get a list of all known cats in the world.

Get a list of all the cats in the world[edit]

Use this URL to get to the Query Service:

  1. Click Examples
  2. Select Cats from the list that appears in the new window
  3. Click Run just below the code box

This has now given us a list of all the famous and infamous cats on the Internet – or at least the ones Wikidata knows about. This is great, provided you are a cat person and not, say, a dog person.

What about dogs?[edit]

If you want to list the dogs instead (or anything else for that matter), there are two ways to edit your query:

  1. Use the Query Helper to change the item from cat to zoo:
  2. Manually edit the query Each item on Wikidata is uniquely identified using a code. This code is Q146 for “cat”. To find the corresponding code for “dog”, you can look it up on Wikidata:
  1. Use autocompletion (will be covered at the end)
  2. Start with "Cat" example, remove "Q146"
  3. Place cursor after "wd:"
  4. Type "Dog" after "wd:"
  5. Press Ctrl+Space
  6. Use up/down arrows; when you hit Enter - it will be substituted with Q144
    Note: search results depend on interface language.
  1. Go to
  2. Type “dog” in the search box
  3. Click on the first result from the drop-down menu
  4. Take note of the item number for “dog” from the Wikidata page that opens (Q144)

To change your query from “cat” to “dog”, simply replace Q146 by Q144 in the SPARQL query editor.

Run the program, and you will be presented with a list of all the famous and infamous dogs on the Internet.

Breaking down the query[edit]

Now let’s analyse the code to generalise this so we can search for any single object (for example, dogs, planets, countries, songs):

SELECT ?item ?itemLabel
	?item wdt:P31 wd:Q146 . 
	SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
  • SELECT A query SELECTs something. What exactly you want to select and display is described later, but for the time being you just state the placeholders (or variables). Here they are called ?item and ?itemLabel. You can tell that something is a variable if it starts with a question mark.
  • ?item This is the list of items we are looking for. In our case that would be the famous cats. Items on Wikidata work in many different languages, almost 300 of them. Because we don’t think that everything in the world is English and all the other languages are just special cases of English, items are stored in a way that works across languages. Every concept gets a number. Q146 is the concept of a cat, Q64 is Berlin in Germany, Q42 is the writer Douglas Adams and so on. Tip: ?item is just a variable name that we decided to use in our query. It could be anything, for example: ?animal.
  • ?itemLabel Humans can digest names in a human language much better than numbers. A label is the name of an item in a human language, e.g. English (or Esperanto, or Japanese, or Zulu…). We want the labels displayed in our query so that Q1371145 gets displayed as “Socks” (the cat belonging to Bill Clinton and family). Precise details about label service in User manual.
  • WHERE {   This is the WHERE clause of the query. Here we define what goes in the placeholders. A WHERE clause starts with an opening curly bracket { and ends with a closing curly bracket }.
  • wdt:P31 This is the predicate of the subject. If you hover over it, you’ll see that it stands for instance of.

  • wd:Q146 . This is the object. If you hover over it, you’ll see that it stands for cat.

All this is quite a mouthful. It may help to express the query in natural language:

“Give me a list of items and their labels, where the items are instances of cat. Then generate the labels to the items in English.”

With the code we used in our examples we showed you a list of all the famous and infamous cats on the Internet. This query can easily be changed to show a similar list for dogs, for example.

Text to SPARQL walkthrough[edit]

The animations below show how to write triplets in natural language, and convert them into SPARQL queries.

The key is to prefix variables with ?, items with wd: and properties with wdt:, and then using the Ctrl+Space (or Ctrl+Alt+Space or Alt+Enter) keyboard shortcut to activate the auto-completion feature that replaces the latter two with the actual Wikidata identifiers.

Diving into the User Interface[edit]

How to change the language that the results are displayed in?[edit]

With this search query tool, you can not only customise and search for simple or aggregated, compound, nested and complex queries but you can also search in ANY language and get results in ANY language too.

  • Change the default language code from “en” (English) to any other language code, say “ja” for Japanese.
SELECT ?item ?itemLabel WHERE
     ?item wdt:P31 wd:Q146 .
     SERVICE wikibase:label { bd:serviceParam wikibase:language "ja" }

How to change the sorting order of the results displayed?[edit]

The order of the items displayed can be easily changed in the interface. Just click on the relevant columns of the search results to customise the sorting order:

How to change the language of the UI?[edit]

Click the word English and select the language from the list displayed:

Notice the UI has changed to the selected language:

But I want pictures of cats! How to search for images?[edit]

So far, we’ve seen the output of the queries displayed as a table. Let’s try for some images:

Click the “Examples” button and select the “Even more cats, with pictures” example. Run the query and cat images should pop up at the lower half of the screen.

This is what displays the result as images instead of a table. You can manually switch how the data is displayed by using the “Display” menu in the lower right corner of the result. Try switching the menu to “Table” to see the same result in a table view.

Let’s have a look at what’s new in the query, compared to the query in the previous chapter. Written in plain English, the query would read “Give me all items that have something to do with cats. Also give me the image for each item. And by the way, display the result as a grid of images, not as a table.”

  • #defaultView:ImageGrid What looks like a comment, is actually an instruction to display the query result as images instead of a table.
  • ?x1 This means that we query for any predicate, not only for “is an instance of”. Think of predicates like “depicts” (P180) or “is named after” (P138). We are not using ?x1 anywhere else in the query, meaning the placeholder stands for “I don’t care what the predicate is, give me all results that somehow have a cat as the subject”
  • OPTIONAL Inside the curly braces you see another statement that adds optional data to the result. The placeholder ?item is reused and coupled with the predicate P18 (meaning “(has) image” and a new placeholder ?pic. Wrapping the statement with OPTIONAL means that items do not necessarily have to have a P18 predicate to show up in the list and that the ?pic placeholder can be empty.

Try modifying the query:

  • Search for dogs instead of cats. Hint: Change the Object part of the first statement in WHERE.
  • Only show items that “depict” cats (you’ll get paintings). Hint: replace ?x1 with something else.
  • Remove the OPTIONAL

There are other ways to show your data, but not all of them are always applicable, which is why some are greyed out in the menu. “Image map” is only selectable if the result data actually contains image URLs. In one of the following chapters you’ll learn how to display items as points on a map.

How to share your query for others to use?[edit]

If you want to share your query with someone else – say, on social media – you can create a short link for your query.

  • Complete your query
  • Click the LINK icon:
  • Copy the URL listed there. This is the URL for the query.

Finding things on maps[edit]

There are more ways to visualize the query results. If the query asks for geocoordinates, the results can be displayed on a map.

Look at this example of lighthouses in Norway. When you run the query, you’ll see red dots that mark the location of lighthouses on the Norwegian coast.

Using the Explorer Dialog[edit]

Once the results of a query have loaded, you can see a magnifying glass icon on each result. Clicking this icon opens the Explorer Dialog.

The explorer dialog by default, displays a single node representing that particular result. Below the node, you can see a toggle that defaults to ‘Outgoing’. This means that clicking on any node will expand all the properties of that node, from the entire Wikidata knowledge base. Toggling this to ‘Incoming’ allows you to see all the incoming links or properties that point to the node. This is a way to explore all the relationships between the various items and their properties using Wikidata.

You can open multiple explorer dialogs at once, allowing you to compare the results of more than one query at once. The explorer dialog also has a toolbar at the bottom right corner which is used to switch between multiple views like Graph, Tree, Map, Table etc.

There is even more[edit]

There are more visualizations for you to explore:

Look through the query examples and try them out!


In the editor, you can press Ctrl+Space at any point in the query and get suggestions for code that might be appropriate; select the right suggestion with the up/down arrow keys, and press Enter to select it.

For example, instead of writing out SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } every time, you can just type SERV, hit Ctrl+Space, and the first suggestion will be that complete label service incantation, ready for use! Just hit Enter to accept it. (The formatting will be a bit different, but that doesn’t matter.)

And autocompletion can also search for you. If you type one of the Wikidata prefixes, like wd: or wdt:, and then just write text afterwards, Ctrl+Space will search for that text on Wikidata and suggest results. wd: searches for items, wdt: for properties. For example, instead of looking up the items for Johann Sebastian Bach (Q1339) and father (P22), you can just type wd:Bach and wdt:fath and then just select the right entry from the autocompletion. (This even works with spaces in the text, e. g. wd:Johann Sebastian Bach.)

NB: For ChromeOS and MacOS users: the shortcuts Ctrl+Alt+Space and Alt+Enter should also work.

Where to go from here[edit]

You can probably find your way around the Wikidata Query Service now. Exploring the interface and changing values in the queries can be a good way to learn how to ask your own questions on Wikidata.