SPARQL/Triples

Introduction
The statement "The sky has the color blue", consists of a subject ("the sky"), a predicate ("has the color"), and an object ("blue").

SPO or "subject, predicate, object" is known as a (Semantic) triple, or commonly referred to in Wikidata as a statement about data.

SPO is also used as a form of basic syntax layout for querying RDF data structures, or any graph database or triplestore, such as the Wikidata Query Service (WDQS).

See also Semantic triple

In Wikidata Query Service (WDQS) triples are used to describe the Query pattern in the  clause of the   statement

?child wdt:P22 wd:Q1339. In this case the triple  specifies that the variable   must have the parent/father Bach.
 * 1) ?child  father   Bach

Any of the triple parts Subject, Predicate and Object may be variables. This makes this selection very versatile.

Triples with the same subject
Aditional variables can be added by adding additional triples. In the simplest case these triples use the same subject.

The first triple selects all the children of Bach. The additional triples links all these triples with a value for gender, birth date and date of death. The variable  links all of them together.

If you look closely at the result you might have noticed that Johann Christoph Friedrich Bach has 2 lines in the list because there are 2 different birth dates, 21 and 23 of June 1732. In his case  resulted into 2 values. See for further details at removing duplicates and modifiers.

OPTIONAL triples
If not all subjects have a value for a certain triple the subject is excluded. To have it included the  keyword comes in handy.

Both children are shown, even if one of the variables (in this case the date of death) is not filled in.

See the chapter OPTIONAL for a full description.

Complex triples
Triples are not limited to one subject. In fact triples can be linked in any thinkable way.

You would for instance be able to list the coordinates of the birth places of the children of Bach

You could even see these birthplaces (Köthen, Leipzig and Weimar) on a map by using

If you click on a red dot you will get additional data as specified above with the variables  and. We had to use,  ,   and all labels should be defined explicitly in the. You can toggle between the Map display and standard table display by the Display drop down list, at the right side of the Run button.

See more about views at Map views or all views

Triples with one variable
An example of a triple with one variable for Subject would be

This will list all Subjects (as variable ) with Predicate  and Object.

An example of a triple with one variable for Predicate would be

This will list all Predicates (as variable ) with Object  and Subject. It shows that he is not only his but also  him

An example of a triple with one variable for Object would be

This will list all Objects (as variable ) with Subject  and Predicate.

Triples with two variables
An example of a triple with 2 variables and only a fixed value for Subject would list all raw information available in Wikidata about Bach

See further at next section with 3 variables for further usage

An example of a triple with 2 variables and only a fixed value for Predicate would list all subjects (probably airports) with an IATA airport code

An usage could be to check for duplicate IATA codes:

An example of a triple with 2 variables and only a fixed value for Object would list all subjects related to Bach

An other possibility of a triple with fixed value for Object would list all subjects with value "ABC", and will show for instance airport Albacete Airport

Triples with three variables
When you would use triples with all 3 as variables (one for Subject, one for Predicate and one for Object) you basically will list out the whole database. This can be done for small databases, and can be used as well to get a rough idea of the available data, on all available properties.

All raw information available in Wikidata about the children of Bach:

The same query but grouped by predicate:

From the query below you can discover triples about the date the Wikidata page was last updated, the total number of statements, the number of sitelinks etc. These are,   and   respectively.