querying RDFs using SPARQL query - sql

Assuming we have a dataset including the following 3 RDF triples. I want to retrieve all these three triples using sparql query.
http://test.com/11/META_C1-INST predicate1 http://test.com/NO_CONTEXT/META_C0059714-INST
http://test.com/23/META_C1-INST predicate1 http://test.com/NO_CONTEXT/META_C0059714-INST
http://test.com/43/META_C1-INST predicate1 http://test.com/NO_CONTEXT/META_C0142817-INST
Here is the query I wrote but I know it is not right:
prefix pred:<http://test.com/>
select distinct *
where {pred:META_C1-INST ?p ?o};
How should I say: "Look for any triples with the subject from http://test.com/??/ where ?? can be anything like 11, 23, S23?

You can transform the URI into a string and then filter based on what it starts with. The keyword you need to use is STRSTARTS:
select distinct *
where{
?s ?p ?o
filter(STRSTARTS(str(?s), "http://test.com/"))
}
or you can use regular expressions with the ^ sign:
select distinct *
where{
?s ?p ?o
FILTER regex(str(?s), "^http://test.com/") .
}

Related

Multiple MINUS clause in a sparql query

I am trying to run the following SPARQL query on the public endpoint of DBPedia:
select distinct ?o ?olabel where {
{ select distinct ?s where {
?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/<http://www.w3.org/2000/01/rdf-schema#subClassOf>{,6} <http://dbpedia.org/ontology/FictionalCharacter>
. ?s <http://dbpedia.org/property/creator> ?oo } limit 100000 }
. ?s <http://dbpedia.org/property/creator> ?o
MINUS { ?o <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/<http://www.w3.org/2000/01/rdf-schema#subClassOf>{,6} <http://dbpedia.org/class/yago/Communicator109610660>}
# MINUS { ?o <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/<http://www.w3.org/2000/01/rdf-schema#subClassOf>{,6} <http://dbpedia.org/class/yago/Creator109614315>}
. ?o <http://www.w3.org/2000/01/rdf-schema#label> ?olabel . FILTER (lang(?olabel)=''||lang(?olabel)='en')
} LIMIT 100
It works ok.
But then I uncomment the commented line.
And then the query fails.
From what I understand, multiple MINUS clauses are authorized in SPARQL.
Can anyone give me pieces of advice to rewrite my query so the multiple MINUS clauses are taken into account by DBPedia?
One of the nice things about standards is that we can look things up in the specifications, so after looking at productions 66, 56, and 54 at https://www.w3.org/TR/sparql11-query/#grammar I can confirm that multiple MINUS clauses are allowed by the standard.

SPARQL Query to second and third level with concat

[I have a Tree Structure with three levels currently I am able to get the first level data from the the query written below I am looking for a query that can help me to concat data for ?o and their respective ?s ?p and ?o and even drill down to third level if needed
I have tried it with construct query but want it in a specific format do trying it out with select and concat
construct{ ?CR ?p ?cOBJECT .}
where {
select ?CR ?p (group_concat(?o;separator=',') as ?cOBJECT)
where{ VALUES ?p {abc:hasPart abc:hasLog abc:Description abc:CRStatus abc:hasSB abc:hasTB}
?CR ?p ?o .
}
GROUP BY ?CR ?p
}

SPARQL: How to get all triples with certain predicate where only one direction exists

I want to query all triples with a certain predicate p. The query should only return triples (s,p,o) where the other direction (o,p,s) does not exist.
How can I make such a query?
That's pretty easy:
SELECT ?s ?p ?o {
?s ?p ?o
MINUS { ?o ?p ?s }
}
FILTER NOT EXISTS instead of MINUS would also work. Replace ?p in the query with the desired predicate, or use something like FILTER (?p=ex:myPredicate) if you want ?p in the result.

Excluding triples based on substring

I have a graph created by sponging web pages. I'm trying to write a query to exclude all triples where the subject is a web page. I can get these triples simply by
FILTER (STRENDS((STR(?i)), "html"))
but what's the best way to exclude them. Basically I need all {?s ?p ?o} minus the subset {?i ?p ?o}.
This
SELECT *
{ ?s ?p ?o
FILTER ( ! STRENDS( STR(?s), "html") )
}
filters the outcome the pattern.

Meaning of the very simple SPARQL query on LinkedGeoData endpoint?

I am a newbie to SPARQL. Though I have read some materials about RDF and SPARQL, I still cannot understand the meaning of the mysterious SPARQL query on the LinkedGeoData SPARQL endpoint
Prefix lgdr:<http://linkedgeodata.org/triplify/>
Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * { ?s ?p ?o . }
Limit 1000
What does this oversimplified where condition ?s ?p ?o mean?
The query you ask about will return 1,000 triples from the endpoint with no filter or condition applied i.e. {?s ?p ?o. } will match any triple.
It's similar to SELECT * FROM aView in SQL if aView was a union of all, or most, of the tables in a SQL database.