Access to European Union open data
EUROPAEU Open Data PortalSample_SPARQL_queries
Menu
Overview Knowledge center Projects Sandbox

Sample SPARQL queries

 

Introduction

The following examples will briefly show you how to use the SPARQL endpoint with GET and POST http methods.

More examples can be found in the section "Linked Data" on EU ODP website: https://data.europa.eu/euodp/en/linked-data

Sample queries from Linked Data page

On the page "Linked Data", you can see some sample queries on (1):

 

When you click on the query in (1), the query will fill in automatically the editor (2). You can also select the format (html, json, xml, ...) and the number of results.

By running the query (3) you will get the result directly in your browser. You can also get the query URL (4) that will give you the query directly encoded in a URL.

Conventions

The following examples are based on the vscode-restclient extension of Visual Studio Code.

Output formats

You can mention the expected output format either by adding the http header parameter Accept or by adding the query parameter format.

The supported output format and corresponding values for the parameter are:

  • HTML: text/html
  • JSON: application/json
  • CSV: text/csv
  • XML: application/sparql-results+xml
  • Spreadsheet: application/vnd.ms-excel
  • JavaScript: application/javascript
  • NTriples: text/plain
  • RDF/XML: application/rdf+xml

GET queries

GET query using the Accept http header

GET https://data.europa.eu/euodp/sparqlep?query=<Encoded SPARQL query>
Accept: <Output format>

or

GET query using the format query parameter

GET https://data.europa.eu/euodp/sparqlep?query=<Encoded SPARQL query>&format=<Output format>

POST queries

POST https://data.europa.eu/euodp/sparqlep
Content-Type: application/x-www-form-urlencoded
Accept: <Output format>
 
query=<SPARQL 
...
...
... Query>

which will be transformed by the REST client into:

POST https://data.europa.eu/euodp/sparqlep HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: <Output format>
Content-Length: <size>
 
query=<Encoded SPARQL query>

 

Sample queries

Retrieve dataset with specific title (eg. 'Register of Commission documents')

SPARQL query

PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX odp:  <http://data.europa.eu/euodp/ontologies/ec-odp#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT  ?Graph ?DatasetTitle  ?Publisher ?DatasetURI WHERE { graph ?Graph  {?DatasetURI a <http://www.w3.org/ns/dcat#Dataset>; dc:publisher ?Publisher; dc:title ?DatasetTitle. FILTER(regex(?DatasetTitle, "Register of Commission documents" ,"i")) }  } LIMIT 10

Query URL generated by EU ODP

As mentioned above, the "Get query URL" button will generate a link like:

https://data.europa.eu/euodp/sparqlep?query=PREFIX+dcat%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23%3E%0D%0APREFIX+odp%3A++%3Chttp%3A%2F%2Fdata.europa.eu%2Feuodp%2Fontologies%2Fec-odp%23%3E%0D%0APREFIX+dc%3A+%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Fterms%2F%3E%0D%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0D%0APREFIX+foaf%3A+%3Chttp%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%3E+SELECT++%3FGraph+%3FDatasetTitle++%3FPublisher+%3FDatasetURI+WHERE+%7B+graph+%3FGraph++%7B%3FDatasetURI+a+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23Dataset%3E%3B+dc%3Apublisher+%3FPublisher%3B+dc%3Atitle+%3FDatasetTitle.+FILTER%28regex%28%3FDatasetTitle%2C+%22Register+of+Commission+documents%22+%2C%22i%22%29%29+%7D++%7D+LIMIT+10&format=text%2Fhtml

The query has been encoded for the URL and the format text/html has been added in the parameters at the end of the line. This link can be directly used in a browser without additional tool.

GET query

The following query returns the same information but in JSON format:

GET query with the output format as query parameter

GET https://data.europa.eu/euodp/sparqlep?query=PREFIX+dcat%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23%3E%0D%0APREFIX+odp%3A++%3Chttp%3A%2F%2Fdata.europa.eu%2Feuodp%2Fontologies%2Fec-odp%23%3E%0D%0APREFIX+dc%3A+%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Fterms%2F%3E%0D%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0D%0APREFIX+foaf%3A+%3Chttp%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%3E+select+distinct+%3Fg+%3Fo+where+%7B+graph+%3Fg+%7B%3Fs+dc%3Atitle+%3Fo.+filter+regex%28%3Fo%2C+%27Statistics%27%2C+%27i%27%29+%7D+%7D+LIMIT+10&format=application%2Fjson

or

GET query with the output format in the http header

GET https://data.europa.eu/euodp/sparqlep?query=PREFIX+dcat%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23%3E%0D%0APREFIX+odp%3A++%3Chttp%3A%2F%2Fdata.europa.eu%2Feuodp%2Fontologies%2Fec-odp%23%3E%0D%0APREFIX+dc%3A+%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Fterms%2F%3E%0D%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0D%0APREFIX+foaf%3A+%3Chttp%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%3E+select+distinct+%3Fg+%3Fo+where+%7B+graph+%3Fg+%7B%3Fs+dc%3Atitle+%3Fo.+filter+regex%28%3Fo%2C+%27Statistics%27%2C+%27i%27%29+%7D+%7D+LIMIT+10
Accept: application/json

POST query

Same example than above but with the POST http method.

POST query like seen in the editor with the output format in the http header

POST https://data.europa.eu/euodp/sparqlep
Content-Type: application/x-www-form-urlencoded
Accept: application/json
 
query=PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX odp:  <http://data.europa.eu/euodp/ontologies/ec-odp#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT  ?Graph ?DatasetTitle  ?Publisher ?DatasetURI WHERE { graph ?Graph  {?DatasetURI a <http://www.w3.org/ns/dcat#Dataset>; dc:publisher ?Publisher; dc:title ?DatasetTitle. FILTER(regex(?DatasetTitle, "Register of Commission documents" ,"i")) }  } LIMIT 10

which will be transformed by the REST client into:

POST query generated by the editor

POST https://data.europa.eu/euodp/sparqlep HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Content-Length: 643
 
query=PREFIX%20dcat:%20%3Chttp://www.w3.org/ns/dcat#%3E%0D%0APREFIX%20odp:%20%20%3Chttp://data.europa.eu/euodp/ontologies/ec-odp#%3E%0D%0APREFIX%20dc:%20%3Chttp://purl.org/dc/terms/%3E%0D%0APREFIX%20xsd:%20%3Chttp://www.w3.org/2001/XMLSchema#%3E%0D%0APREFIX%20foaf:%20%3Chttp://xmlns.com/foaf/0.1/%3E%0D%0ASELECT%20%20?Graph%20?DatasetTitle%20%20?Publisher%20?DatasetURI%20WHERE%20%7B%20graph%20?Graph%20%20%7B?DatasetURI%20a%20%3Chttp://www.w3.org/ns/dcat#Dataset%3E;%20dc:publisher%20?Publisher;%20dc:title%20?DatasetTitle.%20FILTER(regex(?DatasetTitle,%20%22Register%20of%20Commission%20documents%22%20,%22i%22))%20%7D%20%20%7D%20LIMIT%2010

 

Retrieve updated datasets since a certain date

The following example shows how to retrieve the datasets that have been updated since the 8th of April 2019 as CSV.

POST query

POST query like seen in the editor with the output format in the http header

POST https://data.europa.eu/euodp/sparqlep
Content-Type: application/x-www-form-urlencoded
Accept: text/csv
 
query=PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX odp:  <http://data.europa.eu/euodp/ontologies/ec-odp#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?DatasetURI ?DatasetTitle  ?DatasetPublisher
WHERE { graph ?g { 
    ?DatasetURI a <http://www.w3.org/ns/dcat#Dataset>; 
    dc:publisher ?DatasetPublisher; 
    dc:title ?DatasetTitle; 
    dc:modified ?DateModified FILTER(xsd:dateTime(?DateModified) > "2019-04-08"^^xsd:dateTime) 
} }

which will be transformed by the REST client into:

POST query generated by the editor

POST https://data.europa.eu/euodp/sparqlep HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: text/csv
Content-Length: 727
 
query=PREFIX%20dcat:%20%3Chttp://www.w3.org/ns/dcat#%3E%0D%0APREFIX%20odp:%20%20%3Chttp://data.europa.eu/euodp/ontologies/ec-odp#%3E%0D%0APREFIX%20dc:%20%3Chttp://purl.org/dc/terms/%3E%0D%0APREFIX%20xsd:%20%3Chttp://www.w3.org/2001/XMLSchema#%3E%0D%0APREFIX%20foaf:%20%3Chttp://xmlns.com/foaf/0.1/%3E%0D%0ASELECT%20?DatasetURI%20?DatasetTitle%20%20?DatasetPublisher%0D%0AWHERE%20%7B%20graph%20?g%20%7B%20%0D%0A%20%20%20%20?DatasetURI%20a%20%3Chttp://www.w3.org/ns/dcat#Dataset%3E;%20%0D%0A%20%20%20%20dc:publisher%20?DatasetPublisher;%20%0D%0A%20%20%20%20dc:title%20?DatasetTitle;%20%0D%0A%20%20%20%20dc:modified%20?DateModified%20FILTER(xsd:dateTime(?DateModified)%20%3E%20%222019-04-08%22%5E%5Exsd:dateTime)%20%0D%0A%7D%20%7D

Result

CSV

"DatasetURI","DatasetTitle","DatasetPublisher"
"http://data.europa.eu/88u/dataset/YUdZsyJ3THTXt6jnm8JGfw","Intramural R&D expenditure (GERD) by sectors of performance and NUTS 2 regions","http://publications.europa.eu/resource/authority/corporate-body/ESTAT"
"http://data.europa.eu/88u/dataset/YUdZsyJ3THTXt6jnm8JGfw","Interne FuE-Ausgaben nach Leistungssektor und NUTS-2-Regionen","http://publications.europa.eu/resource/authority/corporate-body/ESTAT"
...