How to bind a variable to a queried item in SPARQL

Stefano Borini picture Stefano Borini · Nov 19, 2009 · Viewed 16.7k times · Source

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) }

Answer

scotthenninger picture scotthenninger · Feb 14, 2016

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.