# XQuery/SPARQL Tutorial
### SPARQL interface

The emp-dept RDF can be queried using SPARQL via an XQuery front end to a store provided by Talis. This script supports SPARQL queries and browsing the RDF graph.
The interface expands a query like
that you can run here into
and sends this to the Talis service in a form that can be run that you can run here. The resultant SPARQLQuery Results XML is converted to HTML.

## Example Queries
### List all employees

Run

### List the names of all employees in alphabetical order

Run

### List the employees' name, salary, department number and job

Note that ; in place of . repeats the subject.
Try out this and the following queries here .

### List the employees with salaries over 1000

If the RDF literal is typed, for example as xs:integer as is the case with this generated RDF, then the following query will select employees with a salary greater than 1000:
If the RDF literal is not typed, then the variable must be cast:

### Compute the maximum salary

SPARQL 1.0 lacks min or max, although they are added to some implementations. The following recipe, due to Dean Allemang can be used:
How does this work? We seek a maximum salary of a maximum employee. For such an employee, the OPTIONAL clauses will not match, since there are no employees with a greater salary and thus ?sal will not be bound.
In SPARQL 1.1 max and min are allowed so the query to return the maximum salary becomes

### Employees hired in this millennium

Note that the literal needs to be typed to make this comparison work.

### List the grades of employees

In relational terms, this is a theta-join between the employee and the salgrade tables:

## Abbreviated query syntax

A new prefix simplifies referencing individual resources by their URI
is short for
We could also introduce a default namespace:
and use the abbreviation a for rdf:type:
and if we don't need to return the resource itself, it can be anonymous

## Aggregate features

Aggregation functions like count and sum and the GROUP BY clause are not defined in SPARQL 1.0 although they are available on some services (such as the Talis platform) in advance of standardisation in SPARQL 1.1.

### Count the number of employees in each department
## Generic queries

The uniformity of the triple data model enable us to query the dataset in very general ways, which are useful if we know nothing about the data.

### List all data

This would be impracticable on a realistic dataset, but a sample of the triples can be obtained by limiting the number of triples returned.

### What types are there?

This shows that triples defining the emp vocabulary are in the same dataset.

### What are the ranges of a property?

This query only finds ranges which are instances of a type in the dataset. Sal has a range of xs:integer but it is not easy to discover that with a SPARQL query.

### What properties have a given type as its domain ?
## Schema queries

The presence of schema data enables SPARQL to be used to query this meta-data. The results could be compared with the results by directly querying the data.

### What properties have a domain of a given type?

Note that this has only returned the properties in the empdept vocab, not the foaf name property used in the raw data.

### What types of resources have salaries?

Queries on both the data and the vocab can be made

### What literal properties do MANAGERS have?
## To do

* the example RDF lacks language tags which are required to illustrate lang function
* all queries to be moved to the codelist together with the SQL and XQuery equivalents