Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL equivalence for the SQL IN() operator

Tags:

sparql

Is there a SPARQL equivalence for the SQL IN() operator? I filter my results and now use:

FILTER ( lang(?label) = 'en' )

I want something like:

FILTER ( lang(?label) IN ('en', 'de') )

Is this possible with SPARQL?

like image 673
Martin Schlagnitweit Avatar asked Aug 27 '11 13:08

Martin Schlagnitweit


People also ask

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.

What is SPARQL used for?

SPARQL, short for “SPARQL Protocol and RDF Query Language”, enables users to query information from databases or any data source that can be mapped to RDF. The SPARQL standard is designed and endorsed by the W3C and helps users and developers focus on what they would like to know instead of how a database is organized.

What is SPARQL prefix?

"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.


2 Answers

In SPARQL 1.1 yes.

FILTER ( lang(?label) IN ('en', 'de') )

should work.

Pre-1.1 you'll need a big disjunction:

FILTER ( (lang(?label) = 'en') || (lang(?label) = 'de') )
like image 87
user205512 Avatar answered Oct 06 '22 00:10

user205512


As per official W3C documentation,

IN

boolean  rdfTerm IN (expression, ...)

The IN operator tests whether the RDF term on the left-hand side is found in the values of list of expressions on the right-hand side. The test is done with "=" operator, which tests for the same value, as determined by the operator mapping.

A list of zero terms on the right-hand side is legal.

Errors in comparisons cause the IN expression to raise an error if the RDF term being tested is not found elsewhere in the list of terms.

The IN operator is equivalent to the SPARQL expression:

(lhs = expression1) || (lhs = expression2) || ...

Examples:

enter image description here

So, IN operator accepts list of vales. Hence below is an example, you can try with SPARQL using FILTER +IN syntax:

SELECT ?s1 
WHERE 
{
      ?s1 rdf:type dbpedia-owl:Scientist.
      FILTER (?s1 IN (<http://example.com/#1>,<http://example.com/#2>, <http://example.com/#3>)) 
 }
like image 35
Pratik Avatar answered Oct 05 '22 22:10

Pratik