SPARQL/Wikidata Query Service - Introduction

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Wikidata Query Service SPARQL
Wikidata Query Service - Introduction
Prefixes

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: 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

Screenshot for "A gentle introduction to the Wikidata Query Service" 1.png

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 visual query editor to change the item from cat to dog:Screenshot for "A gentle introduction to the Wikidata Query Service" 5.gif
  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)

Screenshot for "A gentle introduction to the Wikidata Query Service" 3.png

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

Screenshot for "A gentle introduction to the Wikidata Query Service" 7.png

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

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:

Screenshot for "A gentle introduction to the Wikidata Query Service" 6.png

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

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

Screenshot for "A gentle introduction to the Wikidata Query Service" 10.png

Notice the UI has changed to the selected language:

Screenshot for "A gentle introduction to the Wikidata Query Service" 12.png

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.#

Screenshot for "A gentle introduction to the Wikidata Query Service" 2.png

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]

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:
Screenshot for "A gentle introduction to the Wikidata Query Service" 4.png
  • 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.

Screenshot for "A gentle introduction to the Wikidata Query Service" 11.png

There is even more[edit]

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]

See also this Video Querying Wikidata with SPARQL for Absolute Beginners

References[edit]


Wikidata Query Service SPARQL
Wikidata Query Service - Introduction
Prefixes