Nasser
Nasser

Reputation: 2140

What is wrong with my SPARQL query and how can I fix it?

I am learning how to create SPARQL queries. Currently, I am using Dbpedia datasets.

I tried to query about "What are the airports that are in Canada" with the following query:

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

SELECT ?name ?country WHERE {
?name rdf:type <http://dbpedia.org/ontology/Airport>;
?name rdf:type <http://dbpedia.org/ontology/Country>
}
LIMIT 20

I am still confused about building SPARQL queries especially with resources and RDF graphs.

What I need is what is the mistake with the above query?

Thanks

Upvotes: 1

Views: 892

Answers (2)

Manuel Salvadores
Manuel Salvadores

Reputation: 16525

The query you are looking for is something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>

SELECT DISTINCT ?airport ?label WHERE {
?airport rdf:type <http://dbpedia.org/ontology/Airport>;
         rdfs:label ?label;
         dbpedia-owl:location <http://dbpedia.org/resource/Canada> .
}

This query however doesn't return much results and you would be better of with something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>

SELECT DISTINCT ?airport ?label WHERE {
?airport rdf:type <http://dbpedia.org/class/yago/AirportsInOntario> ;
rdfs:label ?label .
}

There are various things wrong in your initial query that imply that you should get a better understanding of SPARQL. You need to revise the way you construct the triple patterns. I recommend you to have a look at the following tutorial:

http://www.cambridgesemantics.com/2008/09/sparql-by-example/

Also you will find exploratory SPARQL queries tremendously helpful:

exploratory SPARQL queries?

Upvotes: 6

DNA
DNA

Reputation: 42617

Your query is currently asking for objects (resources) that have type Airport AND have type Country. Needless to say, there are no results.

You query is also asking for ?country which is completely undefined.

See msalvadores' answer for a correct example...

Upvotes: 0

Related Questions