Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a variable to a queried item in SPARQL

Tags:

sparql

In this simple sparql query I get a list of subjects whose object is 42

SELECT ?v WHERE { ?v ?p 42 }

If I add ?p as a variable

SELECT ?v ?p WHERE { ?v ?p 42 }

I will get two entities per row, the subject and the predicate. What if I wanted three entities, so including the 42? Something like:

SELECT ?v ?p ?m WHERE { ?v ?p (42 as m) }
like image 504
Stefano Borini Avatar asked Nov 19 '09 04:11

Stefano Borini


People also ask

WHAT IS A in SPARQL query?

It's a SPARQL 1.1 property path which describes a route through a graph between two graph nodes, in your case it denotes the inverse path, i.e. from object to subject, thus, it's equivalent to. dbpedia:Stephen_King a ? subtype . with a being just a shortcut for rdf:type.

What types of queries does SPARQL support select all that apply?

SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.

Is SPARQL similar to SQL?

SPARQL and SQL have very similar UNION and MINUS operators, which respectively add and remove solutions from a solution set. Because the datatypes of an SQL table are assumed to be uniform across all rows, care must be taken to align the datatypes of the SELECT.


1 Answers

Another variant is to use BIND, e.g.:

SELECT ?v ?p ?m
WHERE {
  BIND(42 AS ?m)
  ?v ?p ?m
}

The BIND statement simply adds a binding for ?m, which can then be selected for the result set.

like image 176
scotthenninger Avatar answered Oct 23 '22 11:10

scotthenninger