SELECT

Like SQL, SPARQL provides a SELECT query form for selecting or finding data. This section describes the SELECT form.

Syntax

[ PREFIX Clause ]
SELECT [ DISTINCT | REDUCED ] result_expressions_and_variables
[ FROM Clause ]
WHERE Clause
[ Solution Modifiers ]

The optional DISTINCT solution sequence modifier eliminates duplicate results. The REDUCED modifier permits duplicate solutions to be removed but does not guarantee that they are eliminated from the results.

Examples

The following simple SELECT statement queries the sample Tickit data set to return all of the predicates and objects for event100:

SELECT ?predicate ?object
FROM <tickit>
WHERE {
  <event100> ?predicate ?object
}
ORDER BY ?predicate
predicate                                       | object
------------------------------------------------+----------------------
catid                                           | category8
dateid                                          | date2129
eventname                                       | Siegfried
http://www.w3.org/1999/02/22-rdf-syntax-ns#type | event
starttime                                       | 2008-10-30T15:00:00Z
venueid                                         | venue300
6 rows

This simple SELECT statement uses the DISTINCT modifier to list each distinct event name in the Tickit data set:

SELECT DISTINCT ?name
FROM <tickit>
WHERE {
  ?event <eventname> ?name.
}
name
-----------------------------
Adriana Lecouvreur
Eugene Onegin
La Cenerentola (Cinderella)
A Chorus Line
Hairspray
Le Reve
Hedda Gabler
Endgame
Othello
Miss Julie
...
576 rows

This example queries the sample Tickit data set to select the top 10 listings where the price per ticket is greater than $500.00:

SELECT ?listing ?priceperticket
FROM <tickit>
WHERE {
  ?listing <listtime> ?listtime .
  ?listing <priceperticket> ?priceperticket .
  FILTER(?priceperticket > 500.00)
}
ORDER BY desc(?priceperticket)
LIMIT 10
listing       | priceperticket
--------------+----------------
listing209658 |    2500.000000
listing222840 |    2500.000000
listing227412 |    2500.000000
listing215156 |    2500.000000
listing224077 |    2500.000000
listing210898 |    2500.000000
listing224389 |    2500.000000
listing209502 |    2500.000000
listing205037 |    2500.000000
listing225362 |    2500.000000
10 rows