query-6469d700dbedcc28ad6a29273fecb548

rq turtle/ttl

Country QuizHello, everyone! I developed a quiz app about "countries" with a local database dynamically created from Wikidata SPARQL query. This query needs to be improved because more than one line is being returned for 54 out of 197 countries. This query is presented right below. I left the comments to show changes I made that didn't work. Would anyone know how I can improve this query? Thank you

Use at

PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT DISTINCT ?country_label ?capital_label ?currency_label ?population
                ?flag_label ?flag_image ?determination_method_label ?flagStatement
        ?anthem_audio ?official_language_label ?continent_label ?highest_point_label
                ?determination_method_population_label #?endTime2_label
WHERE {
  ?country wdt:P31 wd:Q3624078 .
  FILTER NOT EXISTS { ?country wdt:P31 wd:Q3024240 }
  FILTER NOT EXISTS { ?country wdt:P31 wd:Q28171280 }
  OPTIONAL { ?country wdt:P36 ?capital } .
  OPTIONAL {
    ?country p:P36 ?capitalStatement .
    ?capitalStatement ps:P36 ?capital .
    ?capitalStatement pq:P459 ?determination_method .
    ?determination_method rdfs:label ?determination_method_label .
#    ?capitalStatement pq:P582 ?endTime2 .
#    ?endTime2 rdfs:label ?endTime2_label .
    FILTER (LANG(?determination_method_label) = "en" )

#    FILTER NOT EXISTS { ?capitalStatement pq:P582 ?endTime . }

  } .
  OPTIONAL { ?country wdt:P38 ?currency } .
  OPTIONAL { ?country wdt:P1082 ?population } .
  OPTIONAL {
    ?country p:P1082 ?populationStatement .
    ?populationStatement ps:P1082 ?population .
    ?populationStatement pq:P459 ?determination_method_population .
    ?determination_method_population rdfs:label ?determination_method_population_label .
    FILTER (LANG(?determination_method_population_label) = "en" )
  } .
  OPTIONAL {
    ?country p:P41 ?flagStatement .
    ?flagStatement ps:P41 ?flag_image .
    ?flagStatement wikibase:rank wikibase:PreferredRank .
    FILTER NOT EXISTS { ?flagStatement pq:P582 ?endTime . }
  } .
  OPTIONAL { ?country wdt:P85 ?anthem .
             ?anthem wdt:P51 ?anthem_audio .
  } .

  OPTIONAL { ?country p:P37 ?official_languageStatement .
             ?official_languageStatement ps:P37 ?official_language .
             ?official_languageStatement wikibase:rank wikibase:PreferredRank .
  } .
  OPTIONAL { ?country wdt:P30 ?continent } .
  OPTIONAL { ?country wdt:P610 ?highest_point } .

  SERVICE wikibase:label {
    ?country rdfs:label ?country_label .
    ?capital rdfs:label ?capital_label .
    ?currency rdfs:label ?currency_label .
    ?country rdfs:label ?flag_label .
    ?official_language rdfs:label ?official_language_label .
    ?continent rdfs:label ?continent_label .
    ?highest_point rdfs:label ?highest_point_label .
    bd:serviceParam wikibase:language "en" .
  }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v14("?anthem") v15("?anthem_audio"):::projected v6("?capital") v7("?capitalStatement") v21("?capital_label"):::projected v18("?continent") v25("?continent_label"):::projected v5("?country") v20("?country_label"):::projected v9("?currency") v22("?currency_label"):::projected v8("?determination_method") v4("?determination_method_label"):::projected v12("?determination_method_population") v3("?determination_method_population_label"):::projected v2("?endTime") v1("?flagStatement"):::projected v13("?flag_image"):::projected v23("?flag_label"):::projected v19("?highest_point") v26("?highest_point_label"):::projected v17("?official_language") v16("?official_languageStatement") v24("?official_language_label"):::projected v10("?population"):::projected v11("?populationStatement") c4(["wd:Q28171280"]):::iri c6(["wd:Q3624078"]):::iri c27(["bd:serviceParam"]):::iri c19(["wikibase:PreferredRank"]):::iri c2(["en"]):::literal c5(["wd:Q3024240"]):::iri f0[["not "]] subgraph f0e0["Exists Clause"] e0v1 --"p:direct/P31"--> e0c2 e0v1("?country"):::projected e0c2(["wd:Q28171280"]):::iri end f0--EXISTS--> f0e0 f0 --> v5 f0 --> c3 f0 --> c4 v5 --"p:direct/P31"--> c4 f1[["not "]] subgraph f1e1["Exists Clause"] e1v1 --"p:direct/P31"--> e1c2 e1v1("?country"):::projected e1c2(["wd:Q3024240"]):::iri end f1--EXISTS--> f1e1 f1 --> v5 f1 --> c3 f1 --> c5 v5 --"p:direct/P31"--> c5 v5 --"p:direct/P31"--> c6 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P36".-> v6 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:P36".-> v7 v7 --"p:statement/P36"--> v6 v7 --"p:qualifier/P459"--> v8 v8 --"rdfs:label"--> v4 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P38".-> v9 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P1082".-> v10 end subgraph optional4["(optional)"] style optional4 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:P1082".-> v11 v11 --"p:statement/P1082"--> v10 v11 --"p:qualifier/P459"--> v12 v12 --"rdfs:label"--> v3 end subgraph optional5["(optional)"] style optional5 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:P41".-> v1 v1 --"p:statement/P41"--> v13 v1 --"wikibase:rank"--> c19 end subgraph optional6["(optional)"] style optional6 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P85".-> v14 v14 --"p:direct/P51"--> v15 end subgraph optional7["(optional)"] style optional7 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:P37".-> v16 v16 --"p:statement/P37"--> v17 v16 --"wikibase:rank"--> c19 end subgraph optional8["(optional)"] style optional8 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P30".-> v18 end subgraph optional9["(optional)"] style optional9 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P610".-> v19 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; v5 --"rdfs:label"--> v20 v6 --"rdfs:label"--> v21 v9 --"rdfs:label"--> v22 v5 --"rdfs:label"--> v23 v17 --"rdfs:label"--> v24 v18 --"rdfs:label"--> v25 v19 --"rdfs:label"--> v26 c27 --"wikibase:language"--> c2 end