Skip to content

ASK — yes-or-no queries ​

ASK { … } answers whether the pattern has at least one solution. The query stops at the first match.

Examples use the sample data.

Example ​

pgrdf.sparql() returns one row with the key _ask:

sql
SELECT * FROM pgrdf.sparql($$
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  ASK { ?p a foaf:Person }
$$);
--  {"_ask": "true"}

SELECT * FROM pgrdf.sparql($$
  ASK { <http://example.org/nobody> ?p ?o }
$$);
--  {"_ask": "false"}

As a boolean in SQL ​

The value is the string "true" or "false". Cast it to use it in SQL:

sql
SELECT (sparql->>'_ask')::boolean AS has_people
  FROM pgrdf.sparql($$
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    ASK { ?p a foaf:Person }
  $$);
--  has_people
-- ------------
--  t

That makes ASK a handy check before a load, an update or a downstream step. A property path works in ASK too, for example ASK { ex:alice ex:reportsTo+ ex:erin } (see Property paths).

pgRDF is released under the MIT license. Documentation built with VitePress, served via GitHub Pages.