User:Nastoshka/SPARLQ-Queries

From Wikidata
Jump to navigation Jump to search

Top properties for items of given type[edit]

# Most used properties for Q-items that have a given P31 value
SELECT ?property ?propertyLabel (COUNT(?item) AS ?count) WHERE {
  ?item wdt:P31 wd:Q213924. # Codex -- change it
  ?item ?p ?statement.
  ?property wikibase:claim ?p.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?property ?propertyLabel
ORDER BY DESC(?count)
LIMIT 50
Try it!

Events happened in a certain geo box[edit]

SELECT ?item ?itemLabel ?location ?pointInTime WHERE {
  # Define the modern country (e.g., Russia as wd:Q159)
  VALUES ?modernCountry { wd:Q29 }
  
  # Get the modern country's bounding points
  ?modernCountry wdt:P1332 ?northernPoint .
  ?modernCountry wdt:P1333 ?southernPoint .
  ?modernCountry wdt:P1334 ?easternPoint .
  ?modernCountry wdt:P1335 ?westernPoint .
  
  # Subquery to fetch a limited sample of battle items (would time out otherwise for some countries with lots of items)
  {
    SELECT ?item WHERE {
      ?item wdt:P31/wdt:P279* wd:Q178561. # Filter for instances of battles
    } 
    LIMIT 5000
  }
  
  # Use these points to define the bounding box
  SERVICE wikibase:box {
    ?item wdt:P625 ?location .
    bd:serviceParam wikibase:cornerWest ?westernPoint .
    bd:serviceParam wikibase:cornerEast ?easternPoint .
    bd:serviceParam wikibase:cornerNorth ?northernPoint .
    bd:serviceParam wikibase:cornerSouth ?southernPoint .
  }

  OPTIONAL { ?item wdt:P585 ?pointInTime. }
  
  # Fetch labels for the final result set
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

Items with sitelink but without statements[edit]

SELECT ?item ?article ?d WHERE {
  ?item wikibase:statements 0 .
  ?article schema:about ?item .
  ?article schema:isPartOf <https://it.wikivoyage.org/>.
} LIMIT 100
Try it!

Query Wikidata with MW API Access[edit]

Retrieve information about Arabic-language women poets with more than three site links on Wikidata, excluding those featured in Italian. It lists sorted by the number of links in descending order, limited to the top 500 results.

SELECT ?item ?itemLabel ?itemDescription ?linkcount WHERE {                          
  ?item wdt:P21 wd:Q6581072.                  
  ?item wikibase:sitelinks ?linkcount .                                              
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
  SERVICE wikibase:mwapi {
     bd:serviceParam wikibase:endpoint "en.wikipedia.org";
                     wikibase:api "Generator";
                     mwapi:generator "categorymembers";
                     mwapi:gcmtitle "Category:Arabic-language women poets";
                     mwapi:gcmlimit "max".
     ?item wikibase:apiOutputItem mwapi:item.
  } 
  FILTER NOT EXISTS { ?it schema:about ?item . ?it schema:inLanguage "it" } 
  FILTER(?linkcount > 3 )
  }
ORDER BY DESC (?linkcount)                                                     
LIMIT 500
Try it!