Database Reference
In-Depth Information
SELECT ?firstName ?lastName
WHERE
{
?emp a ex:Employee ; ex:Employee#FirstName ?firstName ;
ex:Employee#LastName ?lastName ; ex:Employee#HireDate ?hireDate .
FILTER( ?hireDate
>
= ''
1992-01-01
''
∧∧
xsd:date &&
?hireDate
<
= ''
1994-12-31
''
∧∧
xsd:date)
}
Filter conditions are Boolean expressions constructed using the logical
connectives
&&
(and),
(or), and
!
(not).
The
FILTER
keyword can be combined with the
NOT EXISTS
keyword to
test the absence of a pattern. For example, the query “First name and last
name of employees without supervisor” reads in SPARQL as follows:
||
SELECT ?firstName ?lastName
WHERE
{
?emp a ex:Employee ; ex:Employee#FirstName ?firstName ;
ex:Employee#LastName ?lastName .
FILTER NOT EXISTS
{
?emp ex:Employee#Supervisor ?sup .
}}
The
OPTIONAL
keyword is used to specify a graph pattern for which the
values will be shown if they are found. For example, the query “First and
last name of employees, along with the first and last name of her supervisor,
if she has one” can be written in SPARQL as follows:
SELECT ?empFirstName ?empLastName ?supFirstName ?supLastName
WHERE
{
?emp a ex:Employee ; ex:Employee#FirstName ?empFirstName ;
ex:Employee#LastName ?empLastName .
OPTIONAL
?emp ex:Employee#Supervisor ?sup .
?sup a ex:Employee ; ex:Employee#FirstName ?supFirstName ;
ex:Employee#LastName ?supLastName .
}}
{
Notice that the
OPTIONAL
keyword behaves in a way similar to an outer
join in SQL.
Aggregation and Sorting in SPARQL
Aggregate functions summarize information from multiple triple patterns
into a single one. SPARQL provides the usual aggregate functions
COUNT
,
SUM
,
MAX
,
MIN
,and
AVG
. In addition, along the lines of SQL, before
summarization, triples may be grouped using the
GROUP BY
keyword, and
then the aggregate function is applied to every group. Furthermore, filtering of
groups may also be performed with the
HAVING
keyword, like it is done with
the
FILTER
clause for ungrouped sets. Finally, the result can be sorted with
the
ORDER BY
clause, where every attribute in the list can be ordered either
in ascending or descending order by specifying
ASC
or
DESC
, respectively.
Consider the query “Total number of orders handled by each employee, in
descending order of number of orders. Only list employees that handled more
than 100 orders.” This query is expressed in SPARQL as follows:
Search WWH ::
Custom Search