SPARQL Query Examples for Querying DBpedia

Are you looking to extract information from DBpedia, the semantic web knowledge base? Do you want to query DBpedia using SPARQL, the RDF query language? If so, you're in the right place! In this article, we'll explore some SPARQL query examples for querying DBpedia, and show you how to use them to retrieve useful information.

What is DBpedia?

DBpedia is a crowd-sourced community effort to extract structured information from Wikipedia and make it available on the web. It provides a structured representation of Wikipedia content, including information about people, places, organizations, films, books, and more. DBpedia is built using the Resource Description Framework (RDF), a standard for representing and linking data on the web.

What is SPARQL?

SPARQL (pronounced "sparkle") is a query language for RDF data. It allows you to retrieve and manipulate data stored in RDF format, using a syntax similar to SQL. SPARQL queries can be used to retrieve information from DBpedia, as well as other RDF datasets on the web.

SPARQL Query Examples

Now that we know what DBpedia and SPARQL are, let's dive into some SPARQL query examples for querying DBpedia. We'll start with some basic queries, and then move on to more complex ones.

Basic Queries

Query 1: Retrieve the name and birth date of Barack Obama

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  <http://dbpedia.org/resource/Barack_Obama> foaf:name ?name ;
                                             dbo:birthDate ?birthdate .
}

This query retrieves the name and birth date of Barack Obama, using the foaf:name and dbo:birthDate properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a single resource (<http://dbpedia.org/resource/Barack_Obama>) and two properties (foaf:name and dbo:birthDate). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate.

Query 2: Retrieve the capital of France

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>

SELECT ?capital
WHERE {
  <http://dbpedia.org/resource/France> dbo:capital ?capital .
}

This query retrieves the capital of France, using the dbo:capital property. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a single resource (<http://dbpedia.org/resource/France>) and one property (dbo:capital). The SELECT clause specifies the variable to retrieve, which in this case is ?capital.

Query 3: Retrieve the population of New York City

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>

SELECT ?population
WHERE {
  <http://dbpedia.org/resource/New_York_City> dbo:populationTotal ?population .
}

This query retrieves the population of New York City, using the dbo:populationTotal property. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a single resource (<http://dbpedia.org/resource/New_York_City>) and one property (dbo:populationTotal). The SELECT clause specifies the variable to retrieve, which in this case is ?population.

Intermediate Queries

Query 4: Retrieve the names and birth dates of all US presidents

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  ?president dbo:position <http://dbpedia.org/resource/President_of_the_United_States> ;
             foaf:name ?name ;
             dbo:birthDate ?birthdate .
}

This query retrieves the names and birth dates of all US presidents, using the dbo:position, foaf:name, and dbo:birthDate properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a variable (?president) and three properties (dbo:position, foaf:name, and dbo:birthDate). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate.

Query 5: Retrieve the names and birth dates of all female US senators

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  ?senator dbo:position <http://dbpedia.org/resource/United_States_Senate> ;
           foaf:name ?name ;
           dbo:birthDate ?birthdate ;
           dbo:gender <http://dbpedia.org/resource/Female> .
}

This query retrieves the names and birth dates of all female US senators, using the dbo:position, foaf:name, dbo:birthDate, and dbo:gender properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a variable (?senator) and four properties (dbo:position, foaf:name, dbo:birthDate, and dbo:gender). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate.

Query 6: Retrieve the names and birth dates of all actors who played James Bond

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  ?actor dbo:starring <http://dbpedia.org/resource/James_Bond> ;
         foaf:name ?name ;
         dbo:birthDate ?birthdate .
}

This query retrieves the names and birth dates of all actors who played James Bond, using the dbo:starring, foaf:name, and dbo:birthDate properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a variable (?actor) and three properties (dbo:starring, foaf:name, and dbo:birthDate). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate.

Advanced Queries

Query 7: Retrieve the names and birth dates of all US presidents, sorted by birth date

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  ?president dbo:position <http://dbpedia.org/resource/President_of_the_United_States> ;
             foaf:name ?name ;
             dbo:birthDate ?birthdate .
}
ORDER BY ?birthdate

This query retrieves the names and birth dates of all US presidents, using the dbo:position, foaf:name, and dbo:birthDate properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a variable (?president) and three properties (dbo:position, foaf:name, and dbo:birthDate). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate. The ORDER BY clause sorts the results by birth date.

Query 8: Retrieve the names and birth dates of all female US senators, sorted by birth date

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  ?senator dbo:position <http://dbpedia.org/resource/United_States_Senate> ;
           foaf:name ?name ;
           dbo:birthDate ?birthdate ;
           dbo:gender <http://dbpedia.org/resource/Female> .
}
ORDER BY ?birthdate

This query retrieves the names and birth dates of all female US senators, using the dbo:position, foaf:name, dbo:birthDate, and dbo:gender properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a variable (?senator) and four properties (dbo:position, foaf:name, dbo:birthDate, and dbo:gender). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate. The ORDER BY clause sorts the results by birth date.

Query 9: Retrieve the names and birth dates of all actors who played James Bond, sorted by birth date

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?birthdate
WHERE {
  ?actor dbo:starring <http://dbpedia.org/resource/James_Bond> ;
         foaf:name ?name ;
         dbo:birthDate ?birthdate .
}
ORDER BY ?birthdate

This query retrieves the names and birth dates of all actors who played James Bond, using the dbo:starring, foaf:name, and dbo:birthDate properties. The PREFIX statements define the namespaces used in the query. The WHERE clause specifies the pattern to match, which in this case is a variable (?actor) and three properties (dbo:starring, foaf:name, and dbo:birthDate). The SELECT clause specifies the variables to retrieve, which in this case are ?name and ?birthdate. The ORDER BY clause sorts the results by birth date.

Conclusion

In this article, we've explored some SPARQL query examples for querying DBpedia, and shown you how to use them to retrieve useful information. We've covered basic queries, intermediate queries, and advanced queries, and demonstrated how to use the PREFIX, WHERE, SELECT, and ORDER BY clauses. We hope this article has been helpful, and that you're now ready to start querying DBpedia using SPARQL!

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Graph ML: Graph machine learning for dummies
Developer Recipes: The best code snippets for completing common tasks across programming frameworks and languages
Loading Screen Tips: Loading screen tips for developers, and AI engineers on your favorite frameworks, tools, LLM models, engines
Knowledge Management Community: Learn how to manage your personal and business knowledge using tools like obsidian, freeplane, roam, org-mode
Cloud Taxonomy - Deploy taxonomies in the cloud & Ontology and reasoning for cloud, rules engines: Graph database taxonomies and ontologies on the cloud. Cloud reasoning knowledge graphs