Langage de requête pour interroger des graphes RDF stockés dans des Triple Stores.

Analogue à SQL pour les bases relationnelles, mais orienté graphe.

Structure générique:

PREFIX prefix: <namespace-uri>
 
SELECT [DISTINCT] variableList
WHERE { pattern }
[GROUP BY]
[ORDER BY]
[LIMIT integer]

Quatre types de requêtes principaux:

SELECT: retourne des résultats en table

ASK: retourne vrai ou faux

CONSTRUCT: construit un nouveau graphe RDF

DESCRIBE: retourne toutes les informations d’une ressource

Exemple SELECT:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX pizza: <http://www.pizza.fr/#>
 
SELECT ?pizza ?price
WHERE {
  ?pizza rdf:type pizza:Pizza .
  ?pizza pizza:price ?price .
}

Exemple ASK:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX esc: <http://www.esc.fr/resource/#>
 
ASK
WHERE {
  ?actor rdf:type esc:Actor .
}

Exemple CONSTRUCT:

PREFIX esc: <http://www.esc.fr/resource/#>
 
CONSTRUCT { ?dir esc:hasDirected esc:Avatar }
WHERE {
  ?dir esc:hasDirected esc:Avatar .
}

Opérateurs de filtrage:

FILTER(?price < 10)
FILTER(CONTAINS(?name, "Margherita"))
FILTER(?date > "2020-01-01"^^xsd:date)

Expression de chemins:

?pizza pizza:topping/rdfs:label ?topping

Équivalent à:

?pizza pizza:topping ?temp .
?temp rdfs:label ?topping .

Requêtes fédérées avec SERVICE:

SERVICE <https://query.wikidata.org/sparql> {
  ?pizza ?p ?o
}

Mise à jour:

INSERT DATA {
  pizza:SalmonPizza rdf:type pizza:Pizza .
}