SPARQL Extensions
in the
Virtuoso Universal Server

OpenLink Software

Scalar Subquery and Existence - Example

This query selects ?x with name Alice such that ?x knows at least one subject with name John .

SELECT ?x
WHERE
  {
    ?x  foaf:name  "Alice"  .
    FILTER
      (
        bif:exists
          ((
             SELECT *
             WHERE
               {
                 ?x  foaf:knows  ?y      .
                 ?y  foaf:name   "John"
               }
          ))
      )
  }

People With Common Interests

This query takes people who have at least one interest in common with the person nicknamed plaid_skirt . Then this counts how many interests each such person shares with plaid_skirt and how many total interests each such person has. The results are sorted on the count of shared interests. This shows a nested DISTINCT subquery, the use of scalar subqueries, and the reference to a selection column in ORDER BY by the column position.

SELECT DISTINCT ?n
  ((
     SELECT COUNT (*)
     WHERE
       {
         ?p   foaf:interest  ?i  .
         ?ps  foaf:interest  ?i
       }
  ))
  ((
     SELECT COUNT (*)
     WHERE
       {
         ?p  foaf:interest  ?i
       }
  ))
WHERE
  {
    ?ps  foaf:nick  "plaid_skirt"@en  .
    {
      SELECT DISTINCT ?p
                      ?psi
      WHERE
        {
          ?p    foaf:interest  ?i .
          ?psi  foaf:interest  ?i
        }
     } .
     FILTER
       (
         ?ps  =  ?psi
       )
       ?p  foaf:nick  ?n
  }
ORDER BY DESC 2
LIMIT 50

Distinctive Shared Interests

This query finds foaf:interests which are shared by only few people. It then returns pairs of people interested in the same specialty where the two people do not know each other. We have both a nested GROUP BY and existence tests.

SELECT ?i
       ?cnt
       ?n1
       ?n2
       ?p1
       ?p2
WHERE
  {
    {
      SELECT ?i
             COUNT (*) AS ?cnt
      WHERE
        {
          ?p  foaf:interest  ?i
        }
      GROUP BY ?i
    }
    FILTER
      (
        ?cnt  >  1
        &&
        ?cnt  <  10
      ) .
    ?p1  foaf:interest  ?i  .
    ?p2  foaf:interest  ?i  .
    FILTER
      (
        ?p1  !=  ?p2
        &&
        !bif:exists
          ((
             SELECT (1)
             WHERE
               {
                 ?p1  foaf:knows  ?p2
               }
          )) &&
        !bif:exists
          ((
             SELECT (1)
             WHERE
               {
                 ?p2  foaf:knows  ?p1
               }
          ))
      ) .
    ?p1  foaf:nick  ?n1  .
    ?p2  foaf:nick  ?n2
  }
ORDER BY ?cnt
LIMIT 10

Path Expressions - Example

In Virtuoso, this query selects all names and optionally mailboxes of friends of all Alices who know a John.

The query requires V6 or higher.

SELECT ?f+>foaf:name
       ?f|>foaf:mbox
WHERE
  {
    ?x  foaf:name   "Alice"  .
    ?x  foaf:knows  ?f       .
    FILTER
      (
        ?f+>foaf:name  =  "John"
      )
  }

The same query could be written:

SELECT ?fname
       ?mbox
WHERE
  {
     ?x   foaf:knows  ?f       .
     ?x   foaf:knows  ?f       .
     OPTIONAL
       {
         ?f  foaf:mbox   ?mbox
       }  .
     ?f   foaf:name   ?fname   .
     ?x   foaf:name   "Alice"  .
     ?x   foaf:knows  ?f2      .
     ?f2  foaf:name   "John"
  }

RDF
SPARQL
SQL
OpenLink
SIOC
WG
SPARUL
ARQ
XQuery
XPath
URI
Vocabulary
IFP
RIF
RDFS
OWL
RDBMS
ODBC
JDBC
GET
POST
QL