Feedback

Share, discover and reuse the Linked Data has never been easier with a LinkedWiki Platform.

Map of countries ordered by the number of their cities with female mayor

Reuse these data in your code

Query, endpoint and code for reusing the same data
https://query.wikidata.org/sparql
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the "ru" label of the country, use "en" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100
Howto write a query SPARQL? (in French)
{{#sparql:PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the "ru" label of the country, use "en" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100
| endpoint = https://query.wikidata.org/sparql
| chart=google.visualization.GeoChart
| options=
| log=2
}}
Howto install LinkedWiki in my wiki? Howto insert this graph in my wiki?
Test this script in a new tab.
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"  async></script>
    </head>
    <body onload="testQuery();">
        <script>
function testQuery(){
    var endpoint = "https://query.wikidata.org/sparql";
    var query = `PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the \"ru\" label of the country, use \"en\" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language \"en\" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100`;

   // $('#bodyContentResearch').append(queryDataset);
    $.ajax({
                url: endpoint,
                dataType: 'json',
                data: {
                    queryLn: 'SPARQL',
                    query: query ,
                    limit: 'none',
                    infer: 'true',
                    Accept: 'application/sparql-results+json'
                },
                success: displayResult,
                error: displayError
        });
}

function displayError(xhr, textStatus, errorThrown) {
    console.log(textStatus);
    console.log(errorThrown);
}

function displayResult(data) {
    $.each(data.results.bindings, function(index, bs) {
        console.log(bs);
        $("body").append(JSON.stringify(bs) + "<hr/>");
    });
}

        </script>
    </body>
</html>
Test this script in a new tab.
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"  async></script>
    </head>
    <body onload="testQuery();">
        <script>
function testQuery(){
    var url = new URL("https://query.wikidata.org/sparql");
    var params = {query:`PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the \"ru\" label of the country, use \"en\" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language \"en\" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100`};

    url.search = new URLSearchParams(params).toString();

    var myHeaders = new Headers();
    myHeaders.append("Accept", "application/sparql-results+json");

    var requestOptions = {
      method: 'GET',
      headers: myHeaders,
      redirect: 'follow'
    };

    fetch(url, requestOptions)
        .then(response => response.json())
        .then(result => displayResult(result))
        .catch(error => displayError(error));
}

function displayError(error) {
    console.log(error);
}

function displayResult(data) {
    data.results.bindings.forEach(bs => {
            console.log(bs);
            $("body").append(JSON.stringify(bs) + "<hr/>");
    });
}
        </script>
    </body>
</html>
Test this script in a new tab (Careful, several charts need a API key).
Howto insert this graph in my html page?
<html>
    <head>
     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css">

     <script
            src="https://code.jquery.com/jquery-3.4.1.min.js"
            integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
            crossorigin="anonymous"> </script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"> </script>
     <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"> </script>

     <script type="text/javascript" src="https://bordercloud.github.io/sgvizler2/sgvizler2/sgvizler2.js" defer> </script>
     <script type="text/javascript" src="https://linkedwiki.com/js/initExampleHTML.js"  defer > </script>
    </head>
<body style="margin:0;">
<div id="sgvzl_example_query"
   data-sgvizler-endpoint="https://query.wikidata.org/sparql"
   data-sgvizler-query="PREFIX bd: &lt;http://www.bigdata.com/rdf#&gt;
PREFIX wikibase: &lt;http://wikiba.se/ontology#&gt;
PREFIX wd: &lt;http://www.wikidata.org/entity/&gt;
PREFIX wdt: &lt;http://www.wikidata.org/prop/direct/&gt;
PREFIX p: &lt;http://www.wikidata.org/prop/&gt;
PREFIX q: &lt;http://www.wikidata.org/prop/qualifier/&gt;
PREFIX v: &lt;http://www.wikidata.org/prop/statement/&gt;

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the &quot;ru&quot; label of the country, use &quot;en&quot; as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language &quot;en&quot; .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100"
    data-sgvizler-chart='google.visualization.GeoChart'
    data-sgvizler-chart-options=''
    data-sgvizler-endpoint_output_format='json'
    data-sgvizler-log='2'
    style='width:100%; height:400px;'
    lang='en'
   />

<script>
/*$(function() {
   sgvizler2.containerDrawAll({
       // Google Api key
       googleApiKey : "GOOGLE_MAP_API_KEY",
       // OpenStreetMap Access Token
       //  https://www.mapbox.com/api-documentation/#access-tokens
       osmAccessToken : "OSM_MAP_API_KEY"
     });
});*/
</script>

</body>
</html>
from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setQuery("""
    PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the "ru" label of the country, use "en" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result)
    #print(result["label"]["value"])
Howto use SPARQL with Python ?
library(SPARQL) # SPARQL querying package
library(ggplot2)

# Step 1 - Set up preliminaries and define query
# Define the data.gov endpoint
    endpoint <- "https://query.wikidata.org/sparql"
# create query statement
    query <- "PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the "ru" label of the country, use "en" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100"
# Step 2 - Use SPARQL package to submit query and save results to a data frame
qd <- SPARQL(endpoint,query,curl_args=list(useragent=R.version.string))
df <- qd$results
SPARQL with R in less than 5 minutes
#!/usr/bin/env ruby
#
# Install sparql for Ruby
#   gem update --system
#   gem install sparql
#
require 'sparql/client'

endpoint = "https://query.wikidata.org/sparql"
sparql = <<-EOT
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the "ru" label of the country, use "en" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100
EOT

#For Wikidata, the method get is required
#For other SPARQL endpoints, the method post is prefered
client = SPARQL::Client.new(endpoint, :method => :get)
rows = client.query(sparql)

puts "Number of rows: #{rows.size}"
for row in rows
  for key,val in row do
    # print "#{key.to_s.ljust(10)}: #{val}\t"
    print "#{key}: #{val}\t"
  end
  print "\n"
end
Doc Ruby for SPARQL 1.1
endpoint = 'https://query.wikidata.org/sparql';

query = ['PREFIX bd: <http://www.bigdata.com/rdf#> '...
'PREFIX wikibase: <http://wikiba.se/ontology#> '...
'PREFIX wd: <http://www.wikidata.org/entity/> '...
'PREFIX wdt: <http://www.wikidata.org/prop/direct/> '...
'PREFIX p: <http://www.wikidata.org/prop/> '...
'PREFIX q: <http://www.wikidata.org/prop/qualifier/> '...
'PREFIX v: <http://www.wikidata.org/prop/statement/> '...
' '...
'SELECT ?countryLabel (count(*) as ?count) WHERE { '...
'  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city '...
'  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement '...
'  ?statement v:P6 ?mayor .     # ... that has the value ?mayor '...
'  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female '...
'  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier '...
'  ?city wdt:P17 ?country   # Also find the country of the city '...
' '...
'  # If available, get the "ru" label of the country, use "en" as fallback '...
'  SERVICE wikibase:label { '...
'    bd:serviceParam wikibase:language "en" . '...
'  } '...
' } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100 '];

url_head = strcat(endpoint,'?query=');
url_query = urlencode(query);
format = 'text/tab-separated-values';
url_tail = strcat('&format=', format);

url = strcat(url_head, url_query, url_tail);

% get the data from the endpoint
query_results = urlread(url);

% write the data to a file so that tdfread can parse it
fid = fopen('query_results.txt','w');
if fid>=0
    fprintf(fid, '%s\n', query_results)
    fclose(fid)
end

% this reads the tsv file into a struct
sparql_data = tdfread('query_results.txt')
Project Github MatlabSPARQL
<?php
require __DIR__ . '/../vendor/autoload.php';
use BorderCloud\SPARQL\SparqlClient;

$endpoint ="https://query.wikidata.org/sparql";
$sp_readonly = new SparqlClient();
$sp_readonly->setEndpointRead($endpoint);
$q = <<<EOD
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?countryLabel (count(*) as ?count) WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city
  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement
  ?statement v:P6 ?mayor .     # ... that has the value ?mayor
  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female
  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
  ?city wdt:P17 ?country   # Also find the country of the city

  # If available, get the "ru" label of the country, use "en" as fallback
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
 } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100
EOD;
$rows = $sp_readonly->query($q, 'rows');
$err = $sp_readonly->getErrors();
if ($err) {
      print_r($err);
      throw new Exception(print_r($err, true));
}

foreach ($rows["result"]["variables"] as $variable) {
        printf("%-20.20s",$variable);
        echo '|';
 }
 echo "\n";

foreach ($rows["result"]["rows"] as $row) {
        foreach ($rows["result"]["variables"] as $variable) {
            printf("%-20.20s", $row[$variable]);
            echo '|';
        }
        echo "\n";
 }
 ?>
Project Github BorderCloud/SPARQL
import com.bordercloud.sparql.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {
        try {
            URI endpoint = new URI("https://query.wikidata.org/sparql");
            String querySelect  = "PREFIX bd: <http://www.bigdata.com/rdf#> \n"
                    + "PREFIX wikibase: <http://wikiba.se/ontology#> \n"
                    + "PREFIX wd: <http://www.wikidata.org/entity/> \n"
                    + "PREFIX wdt: <http://www.wikidata.org/prop/direct/> \n"
                    + "PREFIX p: <http://www.wikidata.org/prop/> \n"
                    + "PREFIX q: <http://www.wikidata.org/prop/qualifier/> \n"
                    + "PREFIX v: <http://www.wikidata.org/prop/statement/> \n"
                    + " \n"
                    + "SELECT ?countryLabel (count(*) as ?count) WHERE { \n"
                    + "  ?city wdt:P31/wdt:P279* wd:Q515 .  # find instances of subclasses of city \n"
                    + "  ?city p:P6 ?statement .      # with a P6 (head of goverment) statement \n"
                    + "  ?statement v:P6 ?mayor .     # ... that has the value ?mayor \n"
                    + "  ?mayor wdt:P21 wd:Q6581072 .     # ... where the ?mayor has P21 (sex or gender) female \n"
                    + "  FILTER NOT EXISTS { ?statement q:P582 ?x }  # ... but the statement has no P582 (end date) qualifier \n"
                    + "  ?city wdt:P17 ?country   # Also find the country of the city \n"
                    + " \n"
                    + "  # If available, get the \"ru\" label of the country, use \"en\" as fallback \n"
                    + "  SERVICE wikibase:label { \n"
                    + "    bd:serviceParam wikibase:language \"en\" . \n"
                    + "  } \n"
                    + " } GROUP BY ?country ?countryLabel ORDER BY DESC(?count) LIMIT 100 \n";
            SparqlClient sc = new SparqlClient(false);
            sc.setEndpointRead(endpoint);
            SparqlResult sr = sc.query(querySelect);
            //sc.printLastQueryAndResult();

            printResult(sr.getModel(),30);
        } catch (URISyntaxException | SparqlClientException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }

    public static void printResult(SparqlResultModel rs , int size) {
        for (String variable : rs.getVariables()) {
            System.out.print(String.format("%-"+size+"."+size+"s", variable ) + " | ");
        }
        System.out.print("\n");
        for (HashMap row : rs.getRows()) {
            for (String variable : rs.getVariables()) {
                System.out.print(String.format("%-"+size+"."+size+"s", row.get(variable)) + " | ");
            }
            System.out.print("\n");
        }
    }
}
For example, with Gradle :
apply plugin: 'java'
apply plugin: 'application'

// This comes out to package + '.' + mainClassName
mainClassName = 'Main'

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}
dependencies {
    compile "com.bordercloud:SPARQL-JAVA:[1.0,)"
}
Project Github BorderCloud/SPARQL-JAVA
Other SPARQL client : RDF4J
You can use the SPARQL clients in Java : BorderCloud/SPARQL-JAVA or RDF4J