See also: Wikidata:SPARQL query service/queries/examples/bibliographic

Generally useful snippets edit

Anything that's an instance of creative work or any subclass thereof
?work wdt:P31/wdt:P279* wd:Q17537576 ;  # instance or subclass of creative work
Inverse of the same thing might be faster
wd:Q17537576 ^wdt:P279*/^wdt:P31 ?work .

Basic queries edit

#Items with a certain property
SELECT ?item ?itemLabel ?typeLabel WHERE {
  ?item wdt:P31 ?type.  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
LIMIT 50

Go to query page

#Items with P6216 qualifed by P793
SELECT ?itemLabel ?copyrightSigEventLabel WHERE { 
  ?item p:P6216 [ pq:P793 $copyrightSigEvent ]. 

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Queries edit

Items linking to a Translation: page edit

#Items that have sitelinks to enWS that start with "Translation:"
SELECT ?item ?typeLabel ?article ?page_titleWS WHERE {
  ?item wdt:P31 ?type.
  
  
  ?article schema:about ?item;
    schema:isPartOf <https://en.wikisource.org/>;
    schema:name ?page_titleWS.
  FILTER(STRSTARTS(?page_titleWS, "Translation:"))
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
LIMIT 50

Go to query page

All subclasses of written work (Q47461344) edit

#All subclasses of "Written Work"
SELECT ?subclass ?desc WHERE {
  ?subclass (wdt:P279+) wd:Q47461344.
  OPTIONAL {
    ?subclass rdfs:label ?desc.
    FILTER((LANG(?desc)) = "en")
  }
}

Go to query page

Editions that don't have an edition of (P629) set edit

# Editions that don't have an "edition of"
SELECT ?edition ?editionLabel ?title ?languageLabel
WHERE 
{
  ?edition wdt:P31 wd:Q3331189.
    MINUS { ?edition wdt:P629 [] } .
    OPTIONAL {
      ?edition wdt:P1476 ?title .
    }
    OPTIONAL {
      ?edition wdt:P407 ?language .
    }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Disambiguation pages entities used as publishers edit

SELECT ?publisher ?publisherLabel ?work WHERE {
  # entities used as publisher
  ?work wdt:P123 ?publisher .
  # but that are actually a disambiguation page
  ?publisher wdt:P31 wd:Q4167410 .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en,fr,de" .
  }
}

Go to query page

Creative works that have an "has edition" but that edition doesn't have "edition of" edit

#title: English Works that have an "edition of" but that edition doesn't refer back
SELECT ?work ?workLabel ?edition ?editionLabel WHERE {
  ?work wdt:P747 ?edition .
  hint:Prior hint:runFirst true.
  ?work wdt:P407 wd:Q1860 . # english works only (can comment line)
  ?work wdt:P31/wdt:P279* wd:Q17537576 .  # instance of creative work or any subclass (or Q7725634: literary work)
  hint:Prior hint:gearing "forward".
  MINUS { ?edition wdt:P629 [] }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Works that have an "edition of" that refers to themselves edit

SELECT ?work ?workLabel ?edition ?editionLabel WHERE {
  ?work wdt:P747 ?edition .
  hint:Prior hint:runFirst true.
  ?work wdt:P31/wdt:P279*  wd:Q17537576 .  # instance of creative work or any subclass (or Q7725634: literary work)
  hint:Prior hint:gearing "forward".
  FILTER ( ?edition = ?work )
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Works that both "have edition" and "are edition of" edit

SELECT ?work ?workLabel ?hasEdition ?hasEditionLabel ?editionOf ?editionOfLabel WHERE {
  ?work wdt:P747 ?hasEdition .
  hint:Prior hint:runFirst true.
  ?work wdt:P629 ?editionOf .
  ?work wdt:P31/wdt:P279*  wd:Q17537576 .  # instance of creative work or any subclass (or Q7725634: literary work)
  hint:Prior hint:gearing "forward".
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Wikisources that do not have local names for themselves edit

SELECT ?item ?itemWikiLangCode ?labelLocal ?labelEn WHERE { 
  ?item wdt:P31 wd:Q15156455 .
  ?item wdt:P424 ?itemWikiLangCode .
  OPTIONAL { ?item rdfs:label ?labelLocal FILTER( LANG(?labelLocal) = ?itemWikiLangCode ) } .
  ?item rdfs:label ?labelEn FILTER( LANG(?labelEn) = 'en' )
} ORDER BY ?labelLocal

Go to query page

Things published in something, but without main subject (P921) edit

SELECT ?item ?label WHERE {
  # Q19020593 = A Compendium of Irish Biography
  ?item wdt:P1433 wd:Q19020593.
  MINUS { ?item wdt:P921 [] } .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?item rdfs:label ?label.
  }
}

Go to query page

Periodicals and collective works edit

Any encyclopedia article without a main subject (P921) edit

# Any encyclopedia article without a main subject (P921)
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q13433827. # P31 is a: Q13433827 encyclopedia article
  MINUS { ?item wdt:P921 [] } . # P921: main subject
  
  # the label service is too slow, so try this
  OPTIONAL {
    ?item rdfs:label ?itemLabel. FILTER( LANG(?itemLabel)="en" )
  }
}

Go to query page

Articles that have volume as a qualifier of published in (P1433) edit

# Articles that have volume set as a qualifier of the published in statement
SELECT DISTINCT ?article ?articleLabel ?publishedinLabel ?volume WITH { 
  SELECT ?article WHERE {
  ?article p:P1433/pq:P478 ?volume .
} } as %i
WHERE
{
  INCLUDE %i
  ?article p:P1433 ?statement .
  ?statement ps:P1433 ?publishedin .
  ?statement pq:P478 ?volume .
  ?article wdt:P31/wdt:P279* wd:Q191067 .  # instance or subclass of article
  hint:Prior hint:gearing "forward".
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Volumes of a periodical with a publication date (P577) set edit

# Volumes of a periodical with a publication date set
SELECT ?item ?itemLabel ?pubdate ?parentLabel
WHERE
{
  ?item (wdt:P31/wdt:P279*) wd:Q1238720 . # item is a volume of some sort
  ?item wdt:P577 ?pubdate . # item has a publication date
  ?item wdt:P179 ?parent . 
  ?parent (wdt:P31/wdt:P279*) wd:Q1002697 . # parent is a periodical
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Authority control and libraries edit

ISILs with wrong codes edit

# Get the ISIL identifier of libraries in the US with the wrong prefix
SELECT ?item ?itemLabel ?isil 
WHERE 
{
  VALUES ?COUNTRIES { wd:Q30 }
  ?item wdt:P791 ?isil;
        wdt:P17 ?COUNTRIES
  FILTER (
    !REGEX(?isil, "^US", "i" )
  )
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Wrong OCLC data edit

# Works that have editions, but also OCLC control numbers (should be OCLC work IDs)
SELECT ?item ?itemLabel ?oclcBib ?edition ?editionLabel ?editionOclc
WHERE
{
  ?item wdt:P747 ?edition.       # find works with editions
  ?item wdt:P243 ?oclcBib .      # find OCLC works with control numbers
  OPTIONAL {
    ?edition wdt:P243 ?editionOclc .       # see if the edition has an OCLC control number too
  }
  hint:Prior hint:runLast true
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

Go to query page

Copyright edit

Editions with a copyright status that does NOT have a determination method edit

# Editions with a copyright status that has a relevant date but not a determination method
SELECT DISTINCT ?work ?workLabel ?methodLabel ?whereLabel ?pdDate ?relevantDate WHERE {
  ?work wdt:P31/wdt:P279* wd:Q3331189 .
  hint:Prior hint:gearing "forward".

  ?work p:P6216 ?statusStatement .
  OPTIONAL {
    ?statusStatement pq:P1001 ?where .
  }
  OPTIONAL {
    ?statusStatement pq:P9905 ?relevantDate .
  }
  OPTIONAL {
    ?statusStatement pq:P3893 ?pdDate.
  }
  MINUS {
    ?statusStatement pq:P459 ?method .
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Editions with a copyright status that has a relevant date but does NOT have all of determination method, jurisdiction and public domain date edit

# Editions with a copyright status that has a relevant date but not a determination method
SELECT DISTINCT ?work ?workLabel ?methodLabel ?whereLabel ?relevantDate ?pdDate WHERE {
  ?work wdt:P31/wdt:P279* wd:Q3331189 .
  hint:Prior hint:gearing "forward".

  ?work p:P6216 ?statusStatement .
  ?statusStatement pq:P9905 ?relevantDate.
  OPTIONAL {
    ?statusStatement pq:P459 ?method .
  }
  OPTIONAL {
    ?statusStatement pq:P1001 ?where .
  }
  OPTIONAL {
    ?statusStatement pq:P3893 ?pdDate.
  }
  MINUS {
    ?statusStatement pq:P459 ?where .
    ?statusStatement pq:P1001 ?method .
    ?statusStatement pq:P3893 ?pdDate .
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

All methods used as a determination method edit

# All methods used as a copyright determination
SELECT ?method ?methodLabel WITH { 
  SELECT DISTINCT ?method WHERE {

    ?work p:P6216 [
      pq:P459 ?method;			# 'no value' for relevant date
    ]
  }
} as %i
WHERE
{
  INCLUDE %i
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Go to query page

Wikitionary edit

Lexeme using a WD item as a sense edit

# look up the Lexeme and lemma for a word that has a given WD items as a sense
SELECT DISTINCT ?lexemeId ?lemma WHERE {
  ?lexemeId dct:language wd:Q1860;
    wikibase:lemma ?lemma;
    ontolex:sense ?sense.
  ?sense wdt:P5137 wd:Q283.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY (LCASE(?lemma))

Go to query page

All senses of a lexeme using a WD item as a sense edit

# look up the Lexeme and lemma for a word that has a given WD items as a sense
# and then list ALL the senses of that lexeme
SELECT DISTINCT ?lexemeId ?lemma ?q_concept ?q_conceptDescription WHERE {
  ?lexemeId dct:language wd:Q1860;
    wikibase:lemma ?lemma;
    ontolex:sense ?sense.
  ?sense wdt:P5137 wd:Q283.
  ?sense wdt:P5137 ?q_concept .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY (LCASE(?lemma))

Go to query page