query-0d5ca6e48f23c48d53776b2a4409c508

rq turtle/ttl

Look for continent first and then countryHi, I have a large query where I search for mathematicians of the world, along with their country and continent, and other optional properties. This is probably very not optimised, but I somehow managed to have all the results in due time, except for the country label, but it times out when I request the country label. I'm really lost in writing SPARQL queries and mainly got there by copy/pasting and trial/error attempts. I would just like to retrieve countries names, and I feel somehow the search is badly performed, it would be smarter to loop over continents, and then loop over countries, but my knowledge is too limited and since the query is already long I don't manage to make it work with trial/error method. Could you help me out?

Use at

PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX p: <http://www.wikidata.org/prop/>
SELECT ?id ?idLabel ?country ?countryLabel ?continent  ?occupation ?parentId ?optionAward ?optionAwardLabel ?mgid ?optionNotable_work ?optionNotable_workLabel ?optionWikipedia_article ?influencer (SAMPLE(?student) AS ?student) (SAMPLE(?img) AS ?img) WHERE {
  ?id wdt:P106 wd:Q170790;
    rdfs:label ?idLabel.
  FILTER((LANG(?idLabel)) = "en")
  ?country rdfs:label ?countryLabel.
  FILTER((LANG(?countryLabel)) = "en")
  ?id wdt:P27 ?country.
  ?country wdt:P30 ?continent.
  OPTIONAL {
    ?optionWikipedia_article schema:about ?id;
      schema:isPartOf <https://en.wikipedia.org/>.
  }
  OPTIONAL { ?id wdt:P18 ?img. }
  OPTIONAL {
    ?id p:P800 _:b102.
    _:b102 ps:P800 ?optionNotable_work.
    ?optionNotable_work rdfs:label ?optionNotable_workLabel.
    FILTER((LANG(?optionNotable_workLabel)) = "en")
  }
  OPTIONAL {
    ?id wdt:P166 ?optionAward.
    ?optionAward rdfs:label ?optionAwardLabel.
    FILTER((LANG(?optionAwardLabel)) = "en")
  }
  OPTIONAL {
    ?id (wdt:P802|wdt:P185) ?student.
    ?student wdt:P106 wd:Q170790.
  }
  OPTIONAL {
    ?id (p:P1066|p:P184) _:b103.
    _:b103 (ps:P1066|ps:P184) ?parentId.
    ?parentId wdt:P106 wd:Q170790.
  }
  OPTIONAL { ?id wdt:P737 ?influencer. }
}
GROUP BY ?id ?idLabel ?country ?countryLabel ?continent    ?occupation ?parentId ?optionAward ?optionAwardLabel ?mgid ?optionNotable_work ?optionNotable_workLabel ?optionWikipedia_article ?influencer

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v7("?continent"):::projected v6("?country"):::projected v3("?countryLabel"):::projected v5("?id"):::projected v4("?idLabel"):::projected v15("?img"):::projected v14("?influencer"):::projected v11("?optionAward"):::projected v1("?optionAwardLabel"):::projected v10("?optionNotable_work"):::projected v2("?optionNotable_workLabel"):::projected v8("?optionWikipedia_article"):::projected v13("?parentId"):::projected v15("?student"):::projected a1((" ")) a2((" ")) c3(["wd:Q170790"]):::iri c9([https://en.wikipedia.org/]):::iri f0[["?countryLabel = 'en'"]] f0 --> v3 f1[["?idLabel = 'en'"]] f1 --> v4 v5 --"p:direct/P106"--> c3 v5 --"rdfs:label"--> v4 v6 --"rdfs:label"--> v3 v5 --"p:direct/P27"--> v6 v6 --"p:direct/P30"--> v7 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v8 -."schema:about".-> v5 v8 --"schema:isPartOf"--> c9 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P18".-> v15 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:P800".-> a1 a1 --"p:statement/P800"--> v10 v10 --"rdfs:label"--> v2 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P166".-> v11 v11 --"rdfs:label"--> v1 end subgraph optional4["(optional)"] style optional4 fill:#bbf,stroke-dasharray: 5 5; subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v5 -."p:direct/P185".-> v15 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v5 --"p:direct/P802"--> v15 end union0r <== or ==> union0l end v15 --"p:direct/P106"--> c3 end subgraph optional5["(optional)"] style optional5 fill:#bbf,stroke-dasharray: 5 5; subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; v5 -."p:P184".-> a2 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; v5 --"p:P1066"--> a2 end union1r <== or ==> union1l end subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; a2 --"p:statement/P184"--> v13 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; a2 --"p:statement/P1066"--> v13 end union2r <== or ==> union2l end v13 --"p:direct/P106"--> c3 end subgraph optional6["(optional)"] style optional6 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P737".-> v14 end bind4[/"sample(?student)"/] v15 --o bind4 bind4 --as--o v15 bind5[/"sample(?img)"/] v15 --o bind5 bind5 --as--o v15