SPARQL/SELECT

The  clause consists of 2 or 3 parts.

The first part is the query result variables. These will be shown when the query is executed. The second part is the  clause with the query pattern. This defines the data selection and generates the variables, The last part are the optional modifiers.

Example

Query result variables
In the above example  and   are the variables. The variables are separated by spaces. A variable can be displayed with another name (Alias) by using, like. Notice that the alias again should be a variable, and the combination should start and end with brackets. PS. For aliases of Labels the label should be defined explicitly in the. Alternatively the variable could be named as requested there as well.

Query pattern
The query pattern specifies the data selection and generates the variables, ?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.

Additional triples can be added, for instance to show gender, birth date and date of death. Each sentence should end with a period. The new variable should be added at the top (query result variables) to display them.

Optional query modifiers
See the chapter modifiers for a full description.

The above query can be sorted by the birth date by adding  after the last curly bracket of the   clause

Removing duplicates
You might have noticed that the above query resulted in 21 records, while Bach had only 20 children. This is caused by 2 entries of Johann Christoph Friedrich Bach, because there are 2 different birth dates, 21 and 23 of June 1732.

Generally it is advised to use  in the  result variables like

removes duplicate entries, but in this case that does not help as the entries are different. To be helpful ALL parameters should have been equal.

What helps is to group by child and combine the values of. Also  and   are grouped to be consistent

The optional query modifier used is, and the variables are combined by using. The  has been modified by using the combined variable , instead of. All labels should be defined explicitly in the.

See the chapter modifiers for a full description.

Another way to remove duplicates it so show only one of the possible values, by using one of the Aggregate functions,  ,   or.

The optional query modifier used is also, and the variables are combined by using. The  has been modified by using the first birth date , instead of. All labels should also be defined explicitly in the.

Adding missing entries
Let's try one of the above queries for Obama (Q76):

This results in NO results although Obama has 2 children. The reason is that to match this query, a potential result (a child) must match all the triples we listed: it must have a gender, and a birth data, and a date of death. If one or more of those properties don't exist, it won’t match. And that’s not what we want in this case: we primarily want a list of all the children – if additional data is available, we’d like to include it, but we don’t want that to limit our list of results.

The solution is to tell WDQS that those triples are :

Now both children are shown, and it is clear that the date of death is not filled in yet, as these children are still alive.

See the chapter OPTIONAL for a full description.

Fool proof: Add missing and remove duplicate entries
A fool proof list of children and their details of any given father would be

This combines the  clause in case a property is not present and the   clause in case of multiple properties.