Cheatsheets:SPARQL: Difference between revisions
Jump to navigation
Jump to search
Fix tables: add {| |} delimiters (were rendering as literal text) (via update-page on MediaWiki MCP Server) |
Fix examples: use P1 (instance of) not P31 (subclass of); real class values Q2/Q3 (via update-page on MediaWiki MCP Server) |
||
| Line 12: | Line 12: | ||
PREFIX wd: <https://wikibase.ronzz.org/entity/> | PREFIX wd: <https://wikibase.ronzz.org/entity/> | ||
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/> | PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/> | ||
SELECT ?item WHERE { ?item wdt: | SELECT ?item WHERE { ?item wdt:P1 wd:Q2 . } LIMIT 10 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 47: | Line 47: | ||
* '''instance of''' is '''P1''' — this instance does not mirror Wikidata's P31 | * '''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 | * '''Q1''' is the "Spike test item" — useful for testing, but it carries no claims | ||
* Typical pattern: <syntaxhighlight lang="sparql" inline>?item wdt:P1 wd: | * Seed classes, all instance-of P1: '''Q2''' quotation content, '''Q3''' code snippet, '''Q4''' mathematical expression, '''Q5''' programming language | ||
* Typical pattern: <syntaxhighlight lang="sparql" inline>?item wdt:P1 wd:Q2 .</syntaxhighlight> | |||
== FILTER, VALUES, BIND == | == FILTER, VALUES, BIND == | ||
| Line 55: | Line 56: | ||
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/> | PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/> | ||
SELECT ?item ?name WHERE { | SELECT ?item ?name WHERE { | ||
VALUES ?type { wd: | VALUES ?type { wd:Q2 wd:Q3 } | ||
?item wdt:P1 ?type . | ?item wdt:P1 ?type . | ||
BIND(STR(?item) AS ?name) | BIND(STR(?item) AS ?name) | ||
| Line 70: | Line 71: | ||
PREFIX wd: <https://wikibase.ronzz.org/entity/> | PREFIX wd: <https://wikibase.ronzz.org/entity/> | ||
PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/> | PREFIX wdt: <https://wikibase.ronzz.org/prop/direct/> | ||
SELECT ?item ?itemLabel WHERE { | SELECT ?item ?itemLabel ?typeLabel WHERE { | ||
?item wdt:P1 ?type . | ?item wdt:P1 ?type . | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr,eo". } | SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr,eo". } | ||
Revision as of 10:52, 19 August 2026
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:P1 wd:Q2 . } 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
- Seed classes, all instance-of P1: Q2 quotation content, Q3 code snippet, Q4 mathematical expression, Q5 programming language
- Typical pattern:
?item wdt:P1 wd:Q2 .
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:Q2 wd:Q3 }
?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 ?typeLabel 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=jsonto the endpoint URL - Test a fresh entity: query it via
?item wdt:P1 ?typefirst, 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
- Help:Contributing/query — querying with SPARQL on this instance
- Wikidata SPARQL tutorial — works verbatim against any Wikibase
- SPARQL 1.1 spec