Introducció a SPARQL a partir de Wikidata

Toni Hermoso Pulido

@Toniher

Amical Wikimedia

SPARQL Protocol

And

RDF Query Language

SPARQL

Resource Description Framework

RDF

Triples

Serialització RDF

Elements de Wikidata

SPARQL

#Gats
SELECT ?item
WHERE 
{
  ?item wdt:P31 wd:Q146 .
}

SPARQL

wdt: Properties

wd: Entities

...

others:

rdfs

schema

PREFIXES

SPARQL

#Gats
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P31 wd:Q146 .
  ?item rdfs:label ?itemLabel .
}

SPARQL

#Gats
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P31 wd:Q146 ;
        rdfs:label ?itemLabel .
}

SPARQL

#Gats
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P31 wd:Q146 ;
        rdfs:label ?itemLabel .
  FILTER(LANG(?itemLabel) = "ca").
}

SPARQL

#Gats
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P31 wd:Q146 .
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "ca". 
  }
}

SPARQL

#Gats
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P31 wd:Q146 .
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "es,fr,ca". 
  }
}
ORDER BY ?itemLabel
LIMIT 10

SPARQL

#Matemàtiques i polítiques

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955;
    rdfs:label ?itemLabel .
  ?item wdt:P21 wd:Q6581072 .
}

SPARQL

#Matemàtiques i polítiques

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955;
    rdfs:label ?itemLabel .
  ?item wdt:P21 wd:Q6581072 .
  FILTER(LANG(?itemLabel) = "ca") .
 }

SPARQL

#Matemàtiques i polítiques

SELECT ?item ?itemLabel ?itemDescription WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955;
    rdfs:label ?itemLabel ;
    schema:description ?itemDescription .
  ?item wdt:P21 wd:Q6581072 .
  FILTER(LANG(?itemLabel) = "ca") .
  FILTER(LANG(?itemDescription) = "ca") .
  
 }

SPARQL

#Matemàtiques i polítiques

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955 .
  ?item wdt:P21 wd:Q6581072 .
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques i polítiques amb imatges

SELECT DISTINCT ?item ?itemLabel ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955 .
  ?item wdt:P21 wd:Q6581072 .
  ?item wdt:P18 ?image .
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques i polítiques amb imatges opcionals

SELECT DISTINCT ?item ?itemLabel ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955 .
  ?item wdt:P21 wd:Q6581072 .
  OPTIONAL {
  	?item wdt:P18 ?image .
  }
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques i polítiques només amb imatges

SELECT DISTINCT ?item ?itemLabel ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955 .
  ?item wdt:P21 wd:Q6581072 .
  FILTER EXISTS {
  	?item wdt:P18 ?image .
  }
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques i polítiques sense imatges
SELECT DISTINCT ?item ?itemLabel ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955 .
  ?item wdt:P21 wd:Q6581072 .
  FILTER NOT EXISTS {
  	?item wdt:P18 ?image .
  }
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques i polítiques amb imatges opcionals 
#nascudes després del 1961

SELECT DISTINCT ?item ?itemLabel ?born ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106 wd:Q170790, wd:Q82955 .
  ?item wdt:P21 wd:Q6581072 ;
        wdt:P569 ?born .
  FILTER (?born >= "1961-01-01T00:00:00Z"^^xsd:dateTime) .
  OPTIONAL {
  	?item wdt:P18 ?image .
  }
  SERVICE wikibase:label 
  { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

# Subclasses de matemàtic

SELECT DISTINCT ?item ?itemLabel WHERE {
  ?item wdt:P279* wd:Q170790;
  SERVICE wikibase:label { 
  	bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques (ampli) amb imatges opcionals 
#nascudes després del 1961

SELECT DISTINCT ?item ?born ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106/wdt:P279* wd:Q170790;
    wdt:P21 wd:Q6581072;
    wdt:P569 ?born.
  FILTER(?born >= "1961-01-01T00:00:00Z"^^xsd:dateTime)
  OPTIONAL { ?item wdt:P18 ?image. }
  SERVICE wikibase:label { 
  	bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques (ampli) amb imatges opcionals 
#nascudes després del 1961 i amb lloc de naixement conegut

SELECT DISTINCT ?item ?born ?birthPlace ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106/wdt:P279* wd:Q170790;
    wdt:P21 wd:Q6581072;
    wdt:P569 ?born ;
    wdt:P19 ?birthPlace .
  FILTER(?born >= "1961-01-01T00:00:00Z"^^xsd:dateTime)
  OPTIONAL { ?item wdt:P18 ?image. }
  SERVICE wikibase:label { 
  	bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

SPARQL

#Matemàtiques (ampli) amb imatges opcionals 
#nascudes després del 1961 i amb lloc de naixement conegut

SELECT DISTINCT ?item ?born ?birthPlace ?coordinate ?image WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P106/wdt:P279* wd:Q170790;
    wdt:P21 wd:Q6581072;
    wdt:P569 ?born ;
    wdt:P19 ?birthPlace .
    ?birthPlace wdt:P625 ?coordinate .
  FILTER(?born >= "1961-01-01T00:00:00Z"^^xsd:dateTime)
  OPTIONAL { ?item wdt:P18 ?image. }
  SERVICE wikibase:label { 
  	bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ca". 
  }
}

Més informació

Introducció a SPARQL amb Wikidata

By Similis.cc

Introducció a SPARQL amb Wikidata

Introducció al llenguatge SPARQL a partir de la infrastructura de Wikidata

  • 1,420