In this page there are a few examples of SPARQL queries got from the Wikidata Query Service and the relative implementation in SPARQLer.

For each example, you can click the "Run!" button to execute the SPARQLer code and read both the automatically generated SPARQL query and the result set, to see what to expect. Please note that most of the examples may take many seconds to display a result.

On execution, $client is always inited as stated aside.

For the full documentation of SPARQLer, see here.

use MadBob\Sparqler\Client;
use MadBob\Sparqler\Resource;
use MadBob\Sparqler\Collection;
use MadBob\Sparqler\Container;
use MadBob\Sparqler\Terms\Iri;
use MadBob\Sparqler\Terms\Raw;
use MadBob\Sparqler\Terms\Variable;
use MadBob\Sparqler\Terms\Aggregate;
use MadBob\Sparqler\Terms\Optional;
use MadBob\Sparqler\Terms\OwnSubject;

$client = new Client([
  'host' => 'https://query.wikidata.org/sparql',
  'omit_prefix' => true,
  'namespaces' => [
    'wd' => 'http://www.wikidata.org/entity/',
    'wdt' => 'http://www.wikidata.org/prop/direct/',
    'wdno' => 'http://www.wikidata.org/prop/novalue/',
    'wikibase' => 'http://wikiba.se/ontology#',
    'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
    'schema' => 'http://schema.org/',
    'p' => 'http://www.wikidata.org/prop/',
    'ps' => 'http://www.wikidata.org/prop/statement/',
    'pq' => 'http://www.wikidata.org/prop/qualifier/',
    'dct' => 'http://purl.org/dc/terms/',
    'ontolex' => 'http://www.w3.org/ns/lemon/ontolex#',
  ],
]);

$httpclient = \EasyRdf\Http::getDefaultHttpClient();
$httpclient->setConfig(['timeout' => 60]);
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q146 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
$query = $client->doSelect([new OwnSubject()])
  ->where('wdt:P31', new Iri('wd:Q146'))
  ->withWikiDataLabels()
  ->get();
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q2934.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
$query = $client->doSelect([new OwnSubject()])
  ->where('wdt:P31', new Iri('wd:Q2934'))
  ->withWikiDataLabels()
  ->get();
SELECT DISTINCT ?horse ?horseLabel ?mother ?father (year(?birthdate) as ?birthyear) (year(?deathdate) as ?deathyear) ?genderLabel
WHERE {
  ?horse wdt:P31/wdt:P279* wd:Q726 .
  OPTIONAL{?horse wdt:P25 ?mother .}
  OPTIONAL{?horse wdt:P22 ?father .}
  OPTIONAL{?horse wdt:P569 ?birthdate .}
  OPTIONAL{?horse wdt:P570 ?deathdate .}
  OPTIONAL{?horse wdt:P21 ?gender .}

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,ar,be,bg,bn,ca,cs,da,de,el,en,es,et,fa,fi,he,hi,hu,hy,id,it,ja,jv,ko,nb,nl,eo,pa,pl,pt,ro,ru,sh,sk,sr,sv,sw,te,th,tr,uk,yue,vec,vi,zh"
  }
}
ORDER BY ?horse
$birth = new Variable();
$death = new Variable();

$query = $client->doSelectDistinct([
    new OwnSubject(),
    new Optional('wdt:P25'),
    new Optional('wdt:P22'),
    new Aggregate('year', $birth, 'birthyear'),
    new Aggregate('year', $death, 'deathyear'),
    new Optional('wdt:P21')
  ])
  ->where('wdt:P31/wdt:P279*', new Iri('wd:Q726'))
  ->whereOptional('wdt:P569', $birth)
  ->whereOptional('wdt:P570', $death)
  ->withWikiDataLabels(['fr','ar','be','bg','bn','ca','cs','da','de','el','en','es','et','fa','fi','he','hi','hu','hy','id','it','ja','jv','ko','nb','nl','eo','pa','pl','pt','ro','ru','sh','sk','sr','sv','sw','te','th','tr','uk','yue','vec','vi','zh'], ['subject', 'wdt:P21'])
  ->orderBy(new OwnSubject())
  ->get();
SELECT ?item ?itemLabel ?pic
WHERE {
  ?item wdt:P31 wd:Q146 .
  ?item wdt:P18 ?pic
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
$query = $client->doSelect([new OwnSubject(), 'wdt:P18'])
  ->where('wdt:P31', new Iri('wd:Q146'))
  ->withWikiDataLabels()
  ->get();
SELECT distinct *
WHERE {
  ?item wdt:P31/wdt:P279* wd:Q16917; wdt:P625 ?geo .
}
$query = $client->doSelectDistinct([new Raw('*')])
  ->where('wdt:P31/wdt:P279*', new Iri('wd:Q16917'))
  ->where('wdt:P625', new Variable())
  ->get();
SELECT distinct *
WHERE {
  ?item wdt:P31 wd:Q1032372; wdt:P625 ?geo .
}
$query = $client->doSelect(['wdt:P625'])
  ->where('wdt:P31', new Iri('wd:Q1032372'))
  ->get();
SELECT (COUNT(*) AS ?count)
WHERE {
  ?item wdt:P31 wd:Q5 .
}
$query = $client->doSelect(['*'])
  ->where('wdt:P31', new Iri('wd:Q5'))
  ->count();
SELECT ?human ?humanLabel
WHERE {
  ?human wdt:P31 wd:Q5 .
  ?human rdf:type wdno:P40 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
$query = $client->doSelect([new OwnSubject()])
  ->where('wdt:P31', new Iri('wd:Q5'))
  ->where('rdf:type', new Iri('wdno:P40'))
  ->withWikiDataLabels()
  ->get();
SELECT DISTINCT ?item ?itemLabel ?itemDescription ?sitelinks
WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P19 wd:Q60;
    wikibase:sitelinks ?sitelinks.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,nl" }
}
ORDER BY DESC(?sitelinks)
$query = $client->doSelect([new OwnSubject(), 'wikibase:sitelinks'])
  ->where('wdt:P31', new Iri('wd:Q5'))
  ->where('wdt:P19', new Iri('wd:Q60'))
  ->withWikiDataLabels()
  ->orderBy('wikibase:sitelinks', 'desc')
  ->get();
SELECT ?item ?itemLabel ?article
WHERE {
  ?article  schema:about ?item ;
    schema:isPartOf <https://species.wikimedia.org/> .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
LIMIT 200
$article = new Variable();

$query = $client->doSelect([new OwnSubject(), $article])
  ->where($article, new Iri('schema:about'), new OwnSubject())
  ->where($article, new Iri('schema:isPartOf'), new Iri('https://species.wikimedia.org/'))
  ->withWikiDataLabels()
  ->limit(200)
  ->get();
SELECT ?author ?authorLabel (COUNT(?paper) AS ?count)
WHERE {
  ?article  schema:about ?author ;
    schema:isPartOf <https://species.wikimedia.org/> .
  ?author wdt:P31 wd:Q5.
  ?paper wdt:P50 ?author.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
GROUP BY ?author ?authorLabel
ORDER BY DESC(?count)
LIMIT 200
$papers = new Variable();

$query = $client->doSelect([new OwnSubject(), new Aggregate('count', $papers)])
  ->whereReverse('schema:about', function($query) {
    $query->where('schema:isPartOf', new Iri('https://species.wikimedia.org/'));
  })
  ->whereReverse('wdt:P50', $papers)
  ->where('wdt:P31', new Iri('wd:Q5'))
  ->withWikiDataLabels()
  ->groupBy([new OwnSubject()])
  ->limit(200)
  ->get();
SELECT ?eyeColorLabel (COUNT(?human) AS ?count)
WHERE {
  ?human wdt:P31 wd:Q5.
  ?human wdt:P1340 ?eyeColor.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?eyeColorLabel
$humans = new Variable();
$subject = new Variable('eyecolor');

$query = $client->doSelect([new Aggregate('count', $humans)])
  ->setMasterSubject($subject)
  ->where($humans, new Iri('wdt:P31'), new Iri('wd:Q5'))
  ->whereReverse('wdt:P1340', $humans)
  ->withWikiDataLabels()
  ->groupBy([new Variable('eyecolorLabel')])
  ->get();
SELECT ?human ?humanLabel
WHERE {
  ?human wdt:P21 ?gender .
  FILTER wikibase:isSomeValue(?gender)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
$gender = new Variable('gender');

$query = $client->doSelect([new OwnSubject()])
  ->where('wdt:P21', $gender)
  ->whereRaw("FILTER wikibase:isSomeValue(?gender)")
  ->withWikiDataLabels()
  ->get();
SELECT ?wikipedia
WHERE {
  ?wikipedia wikibase:wikiGroup "wikipedia".
}
$query = $client->doSelect([new OwnSubject()])
  ->where('wikibase:wikiGroup', "wikipedia")
  ->get();
SELECT DISTINCT ?lang ?name
WHERE {
  ?article schema:about wd:Q5 ;
    schema:inLanguage ?lang ;
    schema:name ?name ;
    schema:isPartOf [ wikibase:wikiGroup "wikipedia" ] .
  FILTER(?lang in ('en', 'uz', 'ru', 'ko')) .
  FILTER (!CONTAINS(?name, ':')) .
}
$query = $client->doSelect(['schema:name', 'schema:inLanguage'])
  ->where('schema:about', new Iri("wd:Q5"))
  ->whereIn('schema:inLanguage', ["en", "uz", "ru", "ko"])
  ->where('schema:isPartOf', new Raw('[ wikibase:wikiGroup "wikipedia" ]'))
  ->get();
SELECT ?item ?itemLabel ?value ?valueLabel
WHERE {
  ?item wdt:P1800 ?value
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 10
$subject = new OwnSubject();
$value = new Variable();

$query = $client->doSelect([$subject, $value])
  ->where('wdt:P1800', $value)
  ->withWikiDataLabels(['en'], [$subject, $value])
  ->limit(10)
  ->get();
SELECT ?lemma ?item
WHERE {
  VALUES ?lemma {
    "Wikipedia"@de
    "Wikidata"@de
    "Berlin"@de
    "Technische Universität Berlin"@de
  }
  ?sitelink schema:about ?item;
    schema:isPartOf <https://de.wikipedia.org/>;
    schema:name ?lemma.
}
$item = new Variable();

$query = $client->doSelect([$item, 'schema:name'])
  ->where('schema:about', $item)
  ->where('schema:isPartOf', new Iri('https://de.wikipedia.org/'))
  ->whereIn('schema:name', [
    new Raw('"Wikipedia"@de'),
    new Raw('"Wikidata"@de'),
    new Raw('"Berlin"@de'),
    new Raw('"Technische Universität Berlin"@de')
  ])
  ->get();
SELECT distinct ?area ?areaLabel (sum(?folkm_ngd) as ?total_folkm)
WHERE {
  ?item wdt:P361 wd:Q297853.
  ?item wdt:P1082 ?folkm_ngd.
  ?area wdt:P31 wd:Q1907114.
  ?area wdt:P527 ?item.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?area ?areaLabel
$pop = new Variable();

$query = $client->doSelect([new Aggregate('SUM', $pop)])
  ->where('wdt:P1082', $pop)
  ->where('wdt:P361', new Iri('wd:Q297853'))
  ->whereReverse('wdt:P527', function($query) {
    $query->where('wdt:P31', new Iri('wd:Q1907114'));
  })
  ->get();
SELECT ?image ?speciesLabel ?mayorLabel ?placeLabel
WHERE {
  ?species wdt:P279* wd:Q622852 .
  ?mayor wdt:P31 ?species .
  ?mayor p:P39 ?node .
  ?node ps:P39 wd:Q30185 .
  ?node pq:P642 ?place .
  optional{?mayor wdt:P18 ?image}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
$place = new Variable();

$query = $client->doSelect([new Optional('wdt:P18'), 'wdt:P31', $place])
  ->where('wdt:P31', function($query) {
    $query->where('wdt:P279*', new Iri('wd:Q622852'));
  })
  ->where('p:P39', function($query) use ($place) {
    $query->where('ps:P39', new Iri('wd:Q30185'))->where('pq:P642', $place);
  })
  ->withWikiDataLabels(['en'], ['subject', 'wdt:P31', $place])
  ->get();
SELECT ?image ?speciesLabel ?mayorLabel ?placeLabel
WHERE {
  VALUES ?species {
    wd:Q144
    wd:Q146
    wd:Q780
  }
  ?mayor wdt:P31 ?species .
  ?mayor p:P39 ?node .
  ?node ps:P39 wd:Q30185 .
  ?node pq:P642 ?place .
  optional{?mayor wdt:P18 ?image}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
$place = new Variable();

$query = $client->doSelect([new Optional('wdt:P18'), 'wdt:P31', $place])
  ->whereIn('wdt:P31', [
    new Iri('wd:Q144'),
    new Iri('wd:Q146'),
    new Iri('wd:Q780')
  ])
  ->where('p:P39', function($query) use ($place) {
    $query->where('ps:P39', new Iri('wd:Q30185'))->where('pq:P642', $place);
  })
  ->withWikiDataLabels(['en'], ['subject', 'wdt:P31', $place])
  ->get();