Shortcuts: WD:RAQ, w.wiki/LX

Wikidata:Request a query

From Wikidata
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Request a query
Fishing in the Wikidata river requires both an idea where to look for fish and a suitable fishing method. If you have the former, this page can help you find the latter.

This is a page where Milad Zare (Q32146616) queries can be requested. Please provide feedback if a query is written for you.

For sample queries, see Examples and Help:Dataset sizing. Property talk pages include also summary queries for these.

For help writing your own queries, or other questions about queries, see Wikidata talk:SPARQL query service/queries and Wikidata:SPARQL query service/query optimization.

Help resources about Wikidata Query Service (Q20950365) and SPARQL: Wikidata:SPARQL query service/Wikidata Query Help and Category:SPARQL.

To report an issue about the Query Service (interface, results views, export...) please see Wikidata:Contact the development team/Query Service and search.
On this page, old discussions are archived. An overview of all archives can be found at this page's archive index. The current archive is located at 2024/08.

List of notable people whose last name is a common English noun

I’m working on a word game and was looking for a list of “notable people” whose names are also nouns. I tried doing the query for it but it seems to be way over my limited Wikidata/SPARQL knowledge.

Here’s what I have so far:

# list of nouns
SELECT
  ?lexeme ?lexemeLabel
  ?lexical_category ?lexical_categoryLabel
WITH {
  SELECT ?lexeme ?lexemeLabel ?lexical_category WHERE {
    ?lexeme a ontolex:LexicalEntry ;
            dct:language wd:Q1860 ; 
            wikibase:lemma ?lexemeLabel .
    OPTIONAL {
      ?lexeme wikibase:lexicalCategory ?lexical_category .
    }
  }
  LIMIT 10000
} AS %results
WHERE {
  INCLUDE %results
  OPTIONAL {        
    ?lexical_category rdfs:label ?lexical_categoryLabel .
    FILTER (LANG(?lexical_categoryLabel) = "en")
  }
}
Try it!

and

# “celebrities”
SELECT DISTINCT ?lastNameLabel
WHERE {
  # Identify celebrities by their occupation
  ?celebrity wdt:P106 ?occupation;
             wdt:P734 ?lastName.
  
  # Filter for notable occupations
  VALUES ?occupation {
    wd:Q33999    # actor
    wd:Q639669   # musician
    wd:Q483501   # artist
    wd:Q36180    # writer
    wd:Q82955    # politician
    wd:Q2066131  # athlete
    wd:Q211236   # celebrity
  }

  # Get labels for readability
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 1000
Try it!
FiP (talk) 13:01, 19 July 2024 (UTC)[reply]

SPARQL query to sum number of seats of lower houses

Hi all, I am a bit new to Wikidata, and to SPARQL even more!

I created Lower houses of the European Union (Q127603696) to specifically list all relevant lower houses in the EU (and the matching Upper houses of the European Union (Q127603941)). I am now trying to find a way to sum the number of seats (P1342) of each of these lower houses, to come up with a single number that I can use for composition boxes, such as in the infobox of this page European People's Party. This would avoid having to recalculate the total number of seats of lower/upper houses whenever one changes.

I tried my luck with the Wikidata_talk:SPARQL_tutorial and the Wikidata Query Service, and even asked on the live chat, but was not able to find the solution, which I believe should not be too complicated. Eventually, I posted on Wikidata:Project chat‎ and a kind soul pointed to this page.

A side question would be: how can I then use this query as a figure on Wikipedia (such as for the infobox above)?

Thanks in advance! Julius Schwarz (talk) 14:55, 19 July 2024 (UTC)[reply]

DISTINCT for character strings

For

SELECT DISTINCT ?item
  (GROUP_CONCAT(DISTINCT ?namelist;        SEPARATOR = ", ") AS ?names)
  (COUNT (DISTINCT ?namelist) AS ?count) WHERE {
  
  VALUES ?item {wd:Q2420611}

  OPTIONAL 
    {
          {?item wdt:P2561 ?namelist} # name
    UNION {?item wdt:P1448 ?namelist} # official name
    UNION {?item wdt:P1449 ?namelist} # nickname
    UNION {?item wdt:P879  ?namelist} # pennant
    UNION {?item skos:altLabel ?namelist. FILTER(LANG(?namelist) = "en")} # aliases
  }

}
GROUP BY ?item
Try it!

I don't want HMS Colossus to be repeated in the output. Vicarage (talk) 12:37, 25 July 2024 (UTC)[reply]

Change lines 2 and 3 to:
  (GROUP_CONCAT(DISTINCT STR(?namelist);        SEPARATOR = ", ") AS ?names)
  (COUNT (DISTINCT STR(?namelist)) AS ?count) WHERE {
Infrastruktur (talk) 14:20, 26 July 2024 (UTC)[reply]
Thanks, sorry I missed your answer. Vicarage (talk) 15:19, 30 July 2024 (UTC)[reply]

Lists all items in Equatorial Guinea using the word "Río" in ceb label

Hi:

I want to fix the watercourses in Equatorial Guinea related with the ceb import. I'm trying this query:

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P17 wd:Q983.

  SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:endpoint "www.wikidata.org";
        wikibase:api "EntitySearch";
        mwapi:search "Río"; 
        mwapi:language "ceb".
      ?item wikibase:apiOutputItem mwapi:item.
  }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en".}
}
LIMIT 10
Try it!

but the results are 0 items, which is obviously wrong and I have no idea how to fix the query :-)

Thanks! —Ismael Olea (talk) 13:12, 27 July 2024 (UTC)[reply]

Measures of item quality

I am contemplating conducting a study where I improve a randomised selection of wikidata items and compare their quality down the track with un-improved items. I already have a draft query capturing the kinds of people I'm interested in at https://www.wikidata.org/wiki/User:Stuartyeates/InterestingPeopleQuery (query is too long for URL shortener). I need to be able to query the quality of items. I'm thinking of things like:

  • How many statements does an item have, right now?
  • How many identifiers does an item have, right now?
  • How many inter-wiki links does an item have, right now?
  • How many statements does an item have, at some point previously?
  • How many identifiers does an item have, at some point previously?
  • How many sitelinks does an item have, at some point previously?
  • When was an item created?
  • How many edits does an item have?
  • How many distinct editors have edited an item?
  • What proportion of statements in an item have a reference?
  • What proportion of statements in an item have a non-wiki reference?

Are there examples somewhere of this kind of thing? Stuartyeates (talk) 09:45, 30 July 2024 (UTC)[reply]

Since I found my own solution:

SELECT ?item ?statements_with_references ?total_statements  ?number_of_references  ?number_of_b_relationhsips  ?number_of_a_relationhsips  ?number_of_b_literals   WHERE {
  BIND(wd:Q42 AS ?item).
  
  {
    SELECT ?item (COUNT(*) AS ?total_statements) WHERE {
      ?item ?p ?statement.
    } GROUP BY ?item
  }
  {
    SELECT ?item (COUNT(*) AS ?statements_with_references) WHERE {
      ?item ?p ?statement.
      ?statement prov:wasDerivedFrom ?reference.
    } GROUP BY ?item
  }
  {
    SELECT ?item (COUNT(DISTINCT ?reference) AS ?number_of_references) WHERE {
       ?item ?p ?statement.
       FILTER(!isLiteral(?statement)).
       ?statement prov:wasDerivedFrom ?reference.
    } GROUP BY ?item
  }
  {
    SELECT ?item (COUNT(DISTINCT ?statement) AS ?number_of_a_relationhsips) WHERE {
       ?statement ?p ?item.
       FILTER(!isLiteral(?statement)).
    } GROUP BY ?item
  }
  {
    SELECT ?item (COUNT(DISTINCT ?statement) AS ?number_of_b_relationhsips) WHERE {
       ?item ?p ?statement.
       FILTER(!isLiteral(?statement)).
    } GROUP BY ?item
  }
  {
    SELECT ?item (COUNT(DISTINCT ?statement) AS ?number_of_b_literals) WHERE {
       ?item ?p ?statement.
       FILTER(isLiteral(?statement)).
    } GROUP BY ?item
  }

}
Try it!

The #edits and editors can be accessed via the API using queries such as:

https://www.wikidata.org/w/api.php?action=query&prop=revisions&titles=Q42&rvprop=ids&rvlimit=max&format=json

cheers. Stuartyeates (talk) 09:59, 1 August 2024 (UTC)[reply]

Label filter with en and mul

At Help_talk:Default_values_for_labels_and_aliases#SPARQL_querying we are discussing the new mul language code, and were wondering what the most efficient filter syntax would be to select the en value if present, then the mul one if not. The suggestion

OPTIONAL { ?c rdfs:label ?labelEN. FILTER ( lang(?labelEN) = 'en' ) }
OPTIONAL { ?c rdfs:label ?labelMUL. FILTER ( lang(?labelMUL) = 'mul' ) }
BIND(IF(BOUND(?labelEN),?labelEN,?labelMUL) AS ?cLabel).

seems clumsy

OPTIONAL { ?c rdfs:label ?label. FILTER (lang(?label) IN ('en', 'mul'))}

is more elegant, but would it return both values or just en or mul? I know you could COALESCE the variables higher up. Vicarage (talk) 15:25, 30 July 2024 (UTC)[reply]

Introducing SpinachBot!

Hi Request a Query forum community! My name is Hal Triedman, and I'm an engineer at the Wikimedia Foundation who has been dabbling in various AI-related engineering tasks.

Today, I'd like to introduce @SpinachBota new, beta version of an AI-powered Wikidata bot that aims to help users like you write complex SPARQL queries, based on natural language queries.

As a user, you can use the bot on-wiki as follows:

{{spinachbot top}}

Hi [[User:SpinachBot|SpinachBot]]! <Insert natural language query about Wikidata content here>


{{spinachbot response}}
Hi! Could you please provide a specific question or query about Wikidata content? This will help me understand what you’re looking for and assist you better.

Here's the [[meta:User:SpinachBot/wikidata:wikidata event 1137667391|trace of reasonings and actions]] for this request so you can see how I came to this conclusion.
If you'd like to provide feedback or iterate on this query, you can tag me in a response below or [https://spinach.genie.stanford.edu continue the conversation] in a chat format. ~~~~
{{spinachbot response end}}
{{spinachbot bottom}}

The bot monitors Wikidata for mentions, and will attempt to respond to your queries inside of the template within a few minutes.

If you'd like to follow-up, provide feedback on a failed attempt, and/or suggest a new direction for the bot to go in, you can just respond in the same thread, like a normal conversation — just be sure to tag the bot so it gets a notification:

{{spinachbot top}}

Hi [[User:SpinachBot|SpinachBot]]! <Insert natural language question about Wikidata content here>

{{spinachbot response}}
<The bot's response to your question>
{{spinachbot response end}}

[[User:SpinachBot|SpinachBot]] — Can you alter that query by doing... <Insert some follow-up here>

{{spinachbot response}}
<The bot's response to your follow-up>
{{spinachbot response end}}

{{spinachbot bottom}}

Here's an example of the bot in action:

And here are some examples of past queries:

Some other notes:

  • This bot is an agent, so each query creates a history of actions taken by the agent, for example: meta:User:SpinachBot/wikidata:wikidata event 1123939947
  • This bot is multilingual (like most large language models) — if you ask a question in a language, it will attempt to answer that question in the same language.
  • This work is an individual effort on my part, in collaboration with researchers from Stanford University. If you'd like to read a draft academic paper about this work, see this arXiv link.
  • If you would like to interact with this bot in a chat format, you can do so at this link (hosted by Stanford): https://spinach.genie.stanford.edu/

Finally: this bot is experimental, and we're looking for your feedback! This forum is full of SPARQL and Wikidata experts, and we want to know where the bot breaks and how we can improve it. Don't hesitate to post about your successes and failures on this thread, or on the bot's talk page.

Thanks so much! HTriedman (WMF) (talk) 19:33, 30 July 2024 (UTC)[reply]

Congratulations for this amazing work.
I've tried to ask to "explain me a query" in the web interface and it generates a very useful answer.
:Can you explain the following SPARQL query : SELECT (AVG(?age) AS ?agemean) WHERE { wd:Q124005357 wdt:P577 ?publication; wdt:P1891 ?signataire. ?signataire wdt:P569 ?naissance. BIND((YEAR(?publication)) - (YEAR(?naissance)) AS ?age) }

. PAC2 (talk) 04:56, 1 August 2024 (UTC)[reply]

Thank you for this. I did a Wikidata project 2 weeks ago with my students and told them: "One day you can ask Wikidata a question and you'll get the answer." I never thought this would be so quick. 😃 Kdkeller (talk) 23:40, 1 August 2024 (UTC)[reply]

sorting labels with an WITH and INCLUDE

This is a cut down version of a much more complicated query, which for speed is coded this way with include logic that is actually more complicated

SELECT DISTINCT ?item 
  (GROUP_CONCAT(DISTINCT ?classlist;    SEPARATOR = "#") AS ?classes)
  WITH {SELECT ?item WHERE {
VALUES ?item {
  wd:Q820477   # mine
}
    
} } AS %i
WHERE {
  {
    SELECT DISTINCT ?item ?class ?classLabel WHERE {
      {
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
        {INCLUDE %i}

        OPTIONAL {
                {?item wdt:P279 ?class}
          UNION {?class wdt:P279 ?item.
            OPTIONAL {?item wdt:P31 ?instance1}
            OPTIONAL {?class wdt:P31 ?instance2}
            FILTER (!(BOUND(?instance1) && BOUND(?instance2) && ?instance1 != ?instance2))
          }
        }
      }
    }
  }
  BIND(CONCAT(?classLabel) AS ?classlist)
}
GROUP BY ?item
ORDER BY ASC(?classes)
Try it!

The problem I have is the ?classlist entries are not sorted as requested, whether I order by ?classes or ?classlist. Normally I would add another SELECT wrapper to do the sort, but I can't get that working with the WITH and INCLUDE logic. Vicarage (talk) 10:55, 1 August 2024 (UTC)[reply]