Project:Example Queries: Difference between revisions

From lgbtDB
Jump to navigation Jump to search
No edit summary
 
(13 intermediate revisions by the same user not shown)
Line 4: Line 4:
PREFIX wdt: <https://lgbtdb.wikibase.cloud/prop/direct/></syntaxhighlight>
PREFIX wdt: <https://lgbtdb.wikibase.cloud/prop/direct/></syntaxhighlight>


'''NOTE THAT ALL QUERIES BELOW ARE CURRENTLY NON-FUNCTIONAL. FUNCTIONING VERSIONS WILL BE ADDED SOON.'''
'''NOTE THAT MANY QUERIES BELOW ARE CURRENTLY NON-FUNCTIONAL. FUNCTIONING VERSIONS WILL BE ADDED SOON. NON-FUNCTIONING QUERIES ARE LABELED AS SUCH.'''
 
For the example queries utilized to populate the Query Service examples, see [[Project:SPARQL/examples|here]].


== Simple Queries ==
== Simple Queries ==
=== Instances and Subclasses ===
=== Instances and Subclasses ===
To retrieve all instances of an item, you can use the following:
To retrieve all instances of an item, you can use a query like this, which returns all instances of websites:


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
WHERE  
WHERE  
{
{
   ?item wdt:P9 wd:Q166.
   ?item wdt:P1 wd:Q179 .
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}</syntaxhighlight>
}</syntaxhighlight>


To retrieve all subclasses of an item, you can use this:
To retrieve all subclasses of an item, you can use this ('''NOT FUNCTIONAL'''):


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
Line 26: Line 28:
}</syntaxhighlight>
}</syntaxhighlight>


In order to retrieve all instances of, and all instances of subclasses of, an item, you can utilize the following query structure:
In order to retrieve all instances of, and all instances of subclasses of, an item, you can utilize the following query structure ('''NOT FUNCTIONAL'''):


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
Line 36: Line 38:


=== Dates ===
=== Dates ===
==== Birth and Death Dates ====
To get all individuals in lgbtDB with birth date and death date information, utilize the following query structure:
<syntaxhighlight lang="sparql">SELECT DISTINCT ?item ?itemLabel ?bdate ?ddate
WHERE
{
  ?item wdt:P141|wdt:P142 [] .
  OPTIONAL { ?item wdt:P141 ?bdate . }
  OPTIONAL { ?item wdt:P142 ?ddate . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}</syntaxhighlight>
==== Dates Covered ====
==== Dates Covered ====
To locate collections which cover a given area of time (between X and Y), utilize the following query structure:
To locate collections which cover a given area of time (between X and Y), utilize the following query structure ('''NOT FUNCTIONAL'''):


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel ?time
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel ?time
Line 48: Line 62:
}</syntaxhighlight>
}</syntaxhighlight>


For example, looking for collections which cover dates between 1900 and 1940:
For example, looking for collections which cover dates between 1900 and 1940 ('''NOT FUNCTIONAL'''):


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel ?time
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel ?time
Line 61: Line 75:
=== Etymons ===
=== Etymons ===
To get all of the terms derived from a given etymon, you can use the following:
To get all of the terms derived from a given etymon, you can use the following:
<syntaxhighlight lang="sparql">SELECT ?l ?lemma ?languageLabel
WHERE
{
  ?l a ontolex:LexicalEntry;
    dct:language ?language;
    wikibase:lemma ?lemma .
  ?l wdt:P32 wd:L127.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}</syntaxhighlight>
=== Gender Identities ===
To get all of the entities considered gender identities, you can use:


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel  
WHERE  
WHERE  
{
{
   ?item wdt:P49 wd:Q21.  
   ?item wdt:P1 wd:Q10639 .
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}</syntaxhighlight>
}</syntaxhighlight>
Line 73: Line 100:
This query allows you to map different version of the Homosaurus (versions 0, 1, 2, and 3) together:
This query allows you to map different version of the Homosaurus (versions 0, 1, 2, and 3) together:


<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel ?val_0 ?val_1 ?val_2 ?val_3
<syntaxhighlight lang="sparql">SELECT DISTINCT ?item ?itemLabel ?val_0 ?val_1 ?val_2 ?val_3
WHERE  
WHERE  
{
{
Line 85: Line 112:


The {{code|inline=y|lang=sparql|OPTIONAL}} argument here keeps in versions where the mappings do not exist in all of them.
The {{code|inline=y|lang=sparql|OPTIONAL}} argument here keeps in versions where the mappings do not exist in all of them.
=== Lexemes ===
==== Dictionary ====
To return all lexemes in English in dictionary order, you can use the following:
<syntaxhighlight lang="sparql">SELECT ?headword ?categoryLabel {
  ?item wikibase:lemma ?headword .
  ?item wikibase:lexicalCategory ?category .
  ?item dct:language wd:Q2 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ASC(UCASE(str(?headword)))</syntaxhighlight>
==== Lexical Categories ====
This query returns the different lexical categories and the number of lexemes in each:
<syntaxhighlight lang="sparql">SELECT ?item ?itemLabel ?count
WITH {
  SELECT ?item (COUNT(*) AS ?count) {
    [] wikibase:lexicalCategory ?item
  } GROUP BY ?item
} AS %stats
WHERE
{
  include %stats.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY DESC(?count)</syntaxhighlight>


== Complex Queries ==
== Complex Queries ==
=== Graphs and Plots ===
=== Graphs and Plots ===
==== Scatter Plot ====
==== Scatter Plot ====
For example, we may want to graph [[Property:P242|Global Acceptance Index]] (P242) in relationship to [[Property:P362|Transgender Rights Score]] (P362) for countries which have both values indicated.
For example, we may want to graph [[Property:P242|Global Acceptance Index]] (P242) in relationship to [[Property:P362|Transgender Rights Score]] (P362) for countries which have both values indicated ('''NOT FUNCTIONAL''').


<syntaxhighlight lang="sparql">SELECT ?country (SAMPLE(?global_acceptance_index) AS ?global_acceptance_index) (SAMPLE(?transgender_rights_score) AS ?transgender_rights_score) ?countryLabel WHERE {
<syntaxhighlight lang="sparql">SELECT ?country (SAMPLE(?global_acceptance_index) AS ?global_acceptance_index) (SAMPLE(?transgender_rights_score) AS ?transgender_rights_score) ?countryLabel WHERE {
Line 108: Line 161:


<syntaxhighlight lang="sparql">SELECT DISTINCT ?lexemeId ?lemma ?sense
<syntaxhighlight lang="sparql">SELECT DISTINCT ?lexemeId ?lemma ?sense
WHERE  
WHERE {
{
   ?lexemeId dct:language wd:Q2 ;
   ?lexemeId dct:language wd:Q2 ;
     wikibase:lemma ?lemma ;
     wikibase:lemma ?lemma ;
     ontolex:sense ?sense .
     ontolex:sense ?sense .
   ?sense wdt:P1/(wdt:P2)* wd:Q9527 .
   ?sense wdt:P1/(wdt:P2)* wd:Q9507 .
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
}</syntaxhighlight>
</syntaxhighlight>


== See Also ==
== See Also ==
* [https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples Wikidata:SPARQL query service/queries/examples]
* [https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples Wikidata:SPARQL query service/queries/examples]
[[Category:Query Service]]

Latest revision as of 15:07, 24 August 2024

This page includes some example queries which can be run using the Query service. Note that all queries in LGBTdb must be prepended with the following prefixes to function:

PREFIX wd: <https://lgbtdb.wikibase.cloud/entity/>
PREFIX wdt: <https://lgbtdb.wikibase.cloud/prop/direct/>

NOTE THAT MANY QUERIES BELOW ARE CURRENTLY NON-FUNCTIONAL. FUNCTIONING VERSIONS WILL BE ADDED SOON. NON-FUNCTIONING QUERIES ARE LABELED AS SUCH.

For the example queries utilized to populate the Query Service examples, see here.

Simple Queries

Instances and Subclasses

To retrieve all instances of an item, you can use a query like this, which returns all instances of websites:

SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P1 wd:Q179 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

To retrieve all subclasses of an item, you can use this (NOT FUNCTIONAL):

SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P14 wd:Q7297.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

In order to retrieve all instances of, and all instances of subclasses of, an item, you can utilize the following query structure (NOT FUNCTIONAL):

SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P9/(wdt:P14)* wd:Q7297.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Dates

Birth and Death Dates

To get all individuals in lgbtDB with birth date and death date information, utilize the following query structure:

SELECT DISTINCT ?item ?itemLabel ?bdate ?ddate
WHERE 
{
  ?item wdt:P141|wdt:P142 [] .
  OPTIONAL { ?item wdt:P141 ?bdate . }
  OPTIONAL { ?item wdt:P142 ?ddate . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Dates Covered

To locate collections which cover a given area of time (between X and Y), utilize the following query structure (NOT FUNCTIONAL):

SELECT ?item ?itemLabel ?time
WHERE
{
  ?item wdt:P9/wdt:P14* wd:Q3666;
    wdt:P220 ?time.
  FILTER (   YEAR(?time) > X && YEAR(?time) < Y  ).
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

For example, looking for collections which cover dates between 1900 and 1940 (NOT FUNCTIONAL):

SELECT ?item ?itemLabel ?time
WHERE
{
  ?item wdt:P9/wdt:P14* wd:Q3666;
    wdt:P220 ?time.
  FILTER (   YEAR(?time) > 1940 && YEAR(?time) < 1990  ).
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

Etymons

To get all of the terms derived from a given etymon, you can use the following:

SELECT ?l ?lemma ?languageLabel
WHERE 
{
  ?l a ontolex:LexicalEntry;
     dct:language ?language;
     wikibase:lemma ?lemma .
  ?l wdt:P32 wd:L127. 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Gender Identities

To get all of the entities considered gender identities, you can use:

SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P1 wd:Q10639 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

External Identifiers

Mapping

This query allows you to map different version of the Homosaurus (versions 0, 1, 2, and 3) together:

SELECT DISTINCT ?item ?itemLabel ?val_0 ?val_1 ?val_2 ?val_3
WHERE 
{
  ?item wdt:P29|wdt:P28|wdt:P27|wdt:P26 [] .
  OPTIONAL { ?item wdt:P29 ?val_0 . }
  OPTIONAL { ?item wdt:P28 ?val_1 . }
  OPTIONAL { ?item wdt:P27 ?val_2 . }
  OPTIONAL { ?item wdt:P26 ?val_3 . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

The Template:Code argument here keeps in versions where the mappings do not exist in all of them.

Lexemes

Dictionary

To return all lexemes in English in dictionary order, you can use the following:

SELECT ?headword ?categoryLabel { 
  ?item wikibase:lemma ?headword .
  ?item wikibase:lexicalCategory ?category .
  ?item dct:language wd:Q2 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ASC(UCASE(str(?headword)))

Lexical Categories

This query returns the different lexical categories and the number of lexemes in each:

SELECT ?item ?itemLabel ?count
WITH {
  SELECT ?item (COUNT(*) AS ?count) { 
    [] wikibase:lexicalCategory ?item 
  } GROUP BY ?item
} AS %stats 
WHERE 
{
  include %stats.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY DESC(?count)

Complex Queries

Graphs and Plots

Scatter Plot

For example, we may want to graph Global Acceptance Index (P242) in relationship to Transgender Rights Score (P362) for countries which have both values indicated (NOT FUNCTIONAL).

SELECT ?country (SAMPLE(?global_acceptance_index) AS ?global_acceptance_index) (SAMPLE(?transgender_rights_score) AS ?transgender_rights_score) ?countryLabel WHERE {
  ?country wdt:P9 wd:Q2383;
    wdt:P242 ?global_acceptance_index_score;
    wdt:P362 ?transgender_rights_score_score.
  BIND(?global_acceptance_index_score AS ?global_acceptance_index).
  BIND(?transgender_rights_score_score AS ?transgender_rights_score).
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?country ?countryLabel

After the query finishes, select the eye icon on the left side above the query result and select "Scatter Chart" in the dropdown list.

Senses

To get all senses marked as American English gay slang terms or instances of subclasses of American English gay slang terms:

SELECT DISTINCT ?lexemeId ?lemma ?sense
WHERE {
  ?lexemeId dct:language wd:Q2 ;
     wikibase:lemma ?lemma ;
     ontolex:sense ?sense .
  ?sense wdt:P1/(wdt:P2)* wd:Q9507 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

See Also