user3048813
user3048813

Reputation: 31

How to create a small SPARQL query for DBpedia?

I'm a SPARQL beginner, I would like to know how to create this small query in SPARQL from DBpedia:

The query is: Getting the topics of a thing (name of person, organisation …)

SELECT DISTINCT ?occupation WHERE {
  ?s <w3.org/2000/01/rdf-schema#label>; 'Madonna'@en . ?occupation dbpedia-owl:occupation ?s 
} 

So I create this query to get the occupation of Madonna, is this correct? In this case Madonna but it could be anything else.

I tried this query but i think this is wrong:

SELECT DISTINCT  ?occupation WHERE { 
  ?s <http://www.w3.org/2000/01/rdf-schema#label> 'Madonna'@en .
  ?s dbpedia-owl:occupation ?occupation
}

I tried this too i think it's correct:

PREFIX res: <http://dbpedia.org/resource/>
SELECT DISTINCT ?string 
WHERE {
  res:Tom_Cruise dbpprop:occupation ?string .
}

It works with Tom_Cruise but not with Madonna or barack_Obama for example.

Upvotes: 1

Views: 1038

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85913

A query like your attempt is a good start:

PREFIX res: <http://dbpedia.org/resource/>
SELECT DISTINCT ?string 
WHERE {
  res:Tom_Cruise dbpprop:occupation ?string .
}

Now that we've got something specific to work from, we can look at the specific problems that it might have. First, I'm going to rewrite it using the same namespace prefixes that the public endpoint web interface supports, so that we can copy and paste to it. I'm also putting the keywords in lower case because I don't like yelling.

select distinct ?string where {
  dbpedia:Tom_Cruise dbpprop:occupation ?string .
}

SPARQL results

Now, you mentioned that

It works with Tom_Cruise but not with Madonna or barack_Obama, for example.

All the data in DBpedia is publicly available for you to browse. If you want to see why there are no results for Madonna, note that dbpedia:Madonna is shorthand for http://dbpedia.org/resource/Madonna and pull up that page in your browser. From the properties listed on that page, you'll see that it's a redirection page (indeed, you'll see the same thing if you go to the corresponding Wikipedia article, http://en.wikipedia.org/wiki/Madonna). You want the IRI http://dbpedia.org/resource/Madonna_(entertainer). Unfortunately, you can't write that directly in a SPARQL query because of the parentheses, so you have to write

select distinct ?string where {
  <http://dbpedia.org/resource/Madonna_(entertainer)> dbpprop:occupation ?string .
}

SPARQL results

Now, there are a couple of problems with barack_Obama: (i) the capitalization needs to be Barack_Obama if you want any results. If you visit http://dbpedia.org/resource/Barack_Obama, though, you'll see that there's no dbpprop:occupation property. There's not much you can do about that; you can't query for data that isn't there. The data that is there that might be useful to you (and of a similar nature) would be dbpedia-owl:office, and dbpedia-owl:profession. For instance

select distinct ?string where {
  dbpedia:Barack_Obama (dbpedia-owl:office|dbpedia-owl:profession) ?string .
}

SPARQL results

Upvotes: 3

Related Questions