HomePractical SamplesSPARQL Queries

SPARQL Queries

Query for Wikidata: Top 100 industries by company count.

Copy and paste the query into Wikidata's query form, or click here and run the query for results.

SELECT ?industryLabel (count(?company) as ?companyCount)

WHERE {

?company wdt:P452 ?industry

SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}

GROUP BY ?industryLabel
ORDER BY DESC(?companyCount)
LIMIT 100

Query for DBpedia: U.S. Presidents by descending birth date.

Copy and paste this into DBpedia's snorql web form or click here for results. As you can see from the results, the data in DBpedia is a bit messy and/or inconsistent with duplicate entries. In order to clean up the results, one would have to change the query from a SELECT DISTINCT to a GROUP BY with the use of aggregate functions to retrieve the desired record in each set of the duplicates along with the other non-duplicate records.

I also find it interesting the Donald Trump does not appear in this list (as of 8.4.2017), since he has not been assigned to the dbc:Presidents_of_the_United_States object via dct:subject property. Is this Wikipedia being political?

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbp: <http://dbpedia.org/property/>

SELECT DISTINCT ?s ?name ?bYear ?dYear ?viaf
WHERE
{

?s dct:subject dbc:Presidents_of_the_United_States .
OPTIONAL { ?s foaf:name ?name .
FILTER (lang(?name)="en") }

?s dbo:birthDate ?bdate .
BIND(xsd:integer(SUBSTR(STR(?bdate),1,4)) AS ?bYear )

OPTIONAL { ?s dbo:deathDate ?ddate .
BIND(xsd:integer(SUBSTR(STR(?ddate),1,4)) AS ?dYear ) }

OPTIONAL { ?s owl:sameAs ?viaf .
FILTER (CONTAINS(STR(?viaf),"viaf")) }

}

ORDER BY ?bYear