Cheatsheets:SPARQL: Difference between revisions

From Wikibase
Jump to navigation Jump to search
Automated edit (via create-page on MediaWiki MCP Server)
 
Fix tables: add {| |} delimiters (were rendering as literal text) (via update-page on MediaWiki MCP Server)
Line 17: Line 17:
== Query structure ==
== Query structure ==


| Part | Role |
{| class="wikitable"
|------|------|
! Part !! Role
| PREFIX | Namespace shorthand; '''must''' be declared (see gotchas) |
|-
| SELECT | Variables to return |
| PREFIX || Namespace shorthand; '''must''' be declared (see gotchas)
| WHERE | Triple patterns in curly braces |
|-
| FILTER / VALUES / BIND | Constraints and computed values |
| SELECT || Variables to return
| LIMIT / OFFSET | Paging (nginx enforces a 300 s cap, see gotchas) |
|-
| WHERE || Triple patterns in curly braces
|-
| FILTER / VALUES / BIND || Constraints and computed values
|-
| LIMIT / OFFSET || Paging (nginx enforces a 300 s cap, see gotchas)
|}


== Instance prefixes ==
== Instance prefixes ==


| Prefix | Expands to | Use for |
{| class="wikitable"
|--------|------------|---------|
! Prefix !! Expands to !! Use for
| wd: | https://wikibase.ronzz.org/entity/ | Items (Q…) and properties (P…) as subjects/values |
|-
| wdt: | https://wikibase.ronzz.org/prop/direct/ | Direct property statements (P…) |
| wd: || https://wikibase.ronzz.org/entity/ || Items (Q…) and properties (P…) as subjects/values
| wikibase: | built-in | The label service, etc. |
|-
| wdt: || https://wikibase.ronzz.org/prop/direct/ || Direct property statements (P…)
|-
| wikibase: || built-in || The label service, etc.
|}


== Basic patterns ==
== Basic patterns ==

Revision as of 10:51, 19 August 2026

Languages: English · français · Esperanto

Quick reference for smart people — part of our dev cheatsheets collection.

Cheatsheet for querying this instance's data with SPARQL against the Wikibase Query Service.

Quick start

The endpoint is https://wikibase.ronzz.org/sparql. A minimal query (returns one column of entity URIs):

PREFIX wd: <https://wikibase.ronzz.org/entity/>
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/>
SELECT ?item WHERE { ?item wdt:P31 wd:Q161 . } LIMIT 10

Query structure

Part Role
PREFIX Namespace shorthand; must be declared (see gotchas)
SELECT Variables to return
WHERE Triple patterns in curly braces
FILTER / VALUES / BIND Constraints and computed values
LIMIT / OFFSET Paging (nginx enforces a 300 s cap, see gotchas)

Instance prefixes

Prefix Expands to Use for
wd: https://wikibase.ronzz.org/entity/ Items (Q…) and properties (P…) as subjects/values
wdt: https://wikibase.ronzz.org/prop/direct/ Direct property statements (P…)
wikibase: built-in The label service, etc.

Basic patterns

  • instance of is P1 — this instance does not mirror Wikidata's P31
  • Q1 is the "Spike test item" — useful for testing, but it carries no claims
  • Typical pattern: ?item wdt:P1 wd:Q161 .

FILTER, VALUES, BIND

PREFIX wd: <https://wikibase.ronzz.org/entity/>
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/>
SELECT ?item ?name WHERE {
  VALUES ?type { wd:Q161 }
  ?item wdt:P1 ?type .
  BIND(STR(?item) AS ?name)
  FILTER(CONTAINS(?name, "Q"))
}
LIMIT 10

Label service

Labels are not in the graph — use wikibase:label:

PREFIX wd: <https://wikibase.ronzz.org/entity/>
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/>
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P1 ?type .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr,eo". }
}
LIMIT 10

The instance's official languages are en, fr, eo — list them in this order.

Aggregation

PREFIX wd: <https://wikibase.ronzz.org/entity/>
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/>
SELECT ?type (COUNT(?item) AS ?n) WHERE {
  ?item wdt:P1 ?type .
}
GROUP BY ?type
ORDER BY DESC(?n)
LIMIT 10

Common tasks

  • Count triples: SELECT (COUNT(?s) AS ?n) WHERE { ?s ?p ?o }
  • JSON output: append &format=json to the endpoint URL
  • Test a fresh entity: query it via ?item wdt:P1 ?type first, then build up

Gotchas

  • The default wd:/wdt: prefixes resolve to wikidata.org, not this instance — always declare them explicitly, or queries silently return 0 results.
  • nginx caps SPARQL requests at 300 s — keep queries small, page with LIMIT/OFFSET.
  • The WDQS updater can lag behind the wiki (known fresh-instance catch-up quirk) — a just-created entity may not appear in SPARQL immediately; re-check later.
  • No Wikidata P-number mirroring — property IDs here (P1, P2, …) are local; use equivalence statements instead of assuming Wikidata numbers.

Further reading