Help:Partial query

From Wikidata
Jump to navigation Jump to search

Partial queries writes sparql fragments to help you build your sparql queries in a more high level way than pure sparql.

List of existing partial queries[edit]

A (maybe incomplete) list is available, a more exhaustive list is available in the category Category:Partial query.

Note that except explicitly specified queries do not explicitly deal with ranks and uses the truthy values simple triples. Some of them deals with full statements, the behavior may vary.

{{Articles in}} / {{Articles not in}}
Helps you find items with (/without) a sitelink (on any projects) of a certain language. For example
{{Articles in wikipedia}} / {{Articles not in wikipedia}}
Helps you find items with (/without) a sitelink on a specific language edition of Wikipedia
{{Id url}}
builds an url from an id by using the "formatter url" statements for the id.
{{Identical values}}
helps you find items with duplicate main values for some statement
{{LabelContains}}
filter items that have some string it their labels
{{Labelize ids}}
similar to {{Query intl}}, includes the query label service for a list of languages
{{Labelled}}
find the label of items without using the query service
{{LabelStartsWith}}
filters items whose label in a language begins by some string
{{Multiple statements}}
selects some items with several statements of the same property
{{Multiple values}}
selects some item with several statements of the same property
{{No statement}}
items with no statements of a specific property
{{Qualified by}}
selects statements that are qualified by a set of qualifiers
{{Query instances}}
find instances of some class(es)
{{Query labels}}
insert a call to the label service to automatically add labels in some user language. If there is a pair of variables (?name, ?nameLabel) in the select clause the one suffixed with Label should be automatically filled by the query service.
{{Query no qualifier}}
get the items with an unqualified statement for some property. And their values, and statements, if needed.
{{Unique values}}
Find item with a unique statement for some property and get the value.
{{ValidAtDate}}
Find non deprecated statements that where the value at some point in time. Useful to get historical datas
{{Value in SI unit}}
Converts quantities in the corresponding SI unit.

TODO :

{{Valid statement}}
{{Best statement}}
{{Deprecated statement}}
{{Preferred statement}}
{{Normal statement}}

Constraint building helpers[edit]

Also, a set of partial query to build {{Complex constraint}}s

{{IfThenConstraint}}
  If some criteria is matched, then the results should also pass the "then" criteria. This template helps selects results that match the "if" but fails the "then".

Obsolete, broken or deprecated[edit]

{{Query badge}}
(currently broken) ? Selects items that have a certain badge, but currently according to the language of the project. Should work by project like {{Articles in wikipedia}}.
{{Query includes}}
(probably obsolete) Includes the default Wikidata prefix for sparql query. Now handled automatically by the query service.

Examples and how to use[edit]

The templates can be used in conjunction with any template that needs sparql input, or as any over template if you just want to see the result. For example we can write a query of all cats with articles on frwiki whose label starts with an "L" using the three partials

all put in a {{Sparql}} template

{{sparql|query=
 select ?cat {
  # cats (Q146 = cat) instances
  {{query instances|?cat|Q146}}
  # with an frwiki article 
  {{articles in wikipedia|?cat|fr}}
  # whose fr label starts with a capital L
  {{LabelStartsWith|?cat|L|fr}}
 }
}}

which gives

select ?cat ?catLabelFr{
  # cats (Q146 = cat) instances
  ?cat wdt:P279*/wdt:P31 wd:Q146 .
  # with an frwiki article 
  ?cat ^schema:about/schema:isPartOf <https://fr.wikipedia.org/> .
  # whose fr label starts with a capital L
  
     ?cat rdfs:label ?catLabelFr filter (lang(?catLabelFr) = "fr"&& strstarts(?catLabelFr, "L"))
 }
Try it!

Technical aspects[edit]

Partial query templates are designed to try to avoid variable collision by naming the variables used by the templates adding suffixes and by taking the names of the variables by parameter. They uses template like {{Sparql var}} to achieve that. When a property id is expected as a parameter, it’s usually possible to use its english label instead of its id, as the templates use the template {{Pid}}. The names in the variables are then the string used, so if you use instance of for instance of (P31) View with SQID the string instance_of will be used in the variable names.