SPARQL/Wikidata Query Service - Introduction

From Wikibooks, open books for an open world
Jump to navigation Jump to search

How to use Wikidata for a simple query[edit | edit source]

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 | edit source]

Use this URL to get to the Query Service: https://query.wikidata.org

  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 | edit source]

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 visual query editor to change the item from cat to dog:
  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. Go to https://www.wikidata.org
  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

Diving into the User Interface[edit | edit source]

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

With this search query tool, you can not only customize 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.
#Cats  
SELECT ?item ?itemLabel WHERE
{
     ?item wdt:P31 wd:Q146 .
     SERVICE wikibase:label { bd:serviceParam wikibase:language "ja" }
}

Try it!

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

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

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

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 | edit source]

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 grayed 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 | edit source]

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 | edit source]

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.


There is even more[edit | edit source]

There are more visualizations for you to explore:

  • Bubble Chart
  • Tree Map
  • Dimensions
  • Graph

Look through the query examples and try them out!


See also[edit | edit source]

See also this Video Querying Wikidata with SPARQL for Absolute Beginners

References[edit | edit source]