How to query specific entities from Wikidata by ID using a SPARQL query? - sparql

I need a SPARQL query returning a specific Wikidata ID or a list of IDs. In real life such query is useless but I need it for testing purposes.
The easiest variant I could come up with was:
SELECT DISTINCT ?s
WHERE
{
?s ?p ?o
FILTER (?s = wd:Q151345).
}
I have to use DISTINCT because ?s ?p ?o matches every triple within Q151345, if I omit it it output the item as many times as many property-value pairs it has.
Is there any easier way?

With the keyword VALUES, it's possible to use multiple instances.
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT ?s
WHERE
{
VALUES ?s { wd:Q151345 wd:Q2996394 }
?s ?p ?o
}
LIMIT 10
Demo : http://linkedwiki.com/query/Query_multiple_instance_of_in_same_query
Doc: https://www.w3.org/TR/sparql11-query/#inline-data

#greatvovan You can try
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT DISTINCT ?s WHERE {
SELECT ?s WHERE
{
VALUES ?s { wd:Q151345 wd:Q2996394 }
?s ?p ?o
}
}

Related

Try to look for all space missions in the solar system which have become a satellite of their target

The DBPedia SPARQL endpoint doesn't give any results.
What is wrong?
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
SELECT distinct ?s ?o
FROM <http://dbpedia.org/>
WHERE{
?s dcterms:subject/skos:broader*
dbc:Discovery_and_exploration_of_the_Solar_System ;
dbp:satelliteOf ?o .
}

wikidata sparql query using string

I would like to query wikidata in order to obtain entries based_on some technologies (blockchain, solid, RDF and so on). These potential technologies would change dynamically, so that, I inject them as literals from a piece of code. The point is that I get no results when trying injecting literals (strings).
In the next piece of SPARQL, you can see five different approaches I tried (all of them fail), through https://query.wikidata.org/
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX xsd: <www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item
WHERE {
#test1 ?item wdt:P144 ?o FILTER (str(?o) = 'blockchain' ) .
#test2 ?item wdt:P144 ?o FILTER (regex (?o,'blockchain')) .
#test3 ?item wdt:P144 ?o FILTER (regex (?o,'^blockchain$')) .
#test4 ?item wdt:P144 'blockchain' .
#test5 ?item wdt:P144 "blockchain" .
}
Any clue?
Thx in advance for your time and support.

How to query dbpedia.org with sparql

I'm very new in OpenData and try to write a query in SPARQL.
My goal is to get data for following set of criteria:
- Category: Home_automation
- select all items from type "Thing"
- with at least one entry in "is Product of"
- that have a picture-url with a German description
I tried the following:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
WHERE {
cat:Home_automation skos:broader ?x
}
But now I don't know how to add the other filters to the where clause.
I tried to user broader:... to get the items, but I think that was the wrong direction.
I tested the queries with: https://dbpedia.org/sparql
The result should be:
| (label) | (url)
|--------------------------|-----------------------------------
|"Kurzzeitwecker"#de | urls to the picture of the device
|"Staubsauger"#de | -||-
|"Waschmaschine"#de | -||-
|"Geschirrspülmaschine"#de | -||-
Does anyone have some tips please?
UPDATE: new query:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s ?label WHERE {
?s ?p cat:Home_automation .
?s rdf:type owl:Thing .
?s rdfs:label ?label
FILTER (LANG(?label)='de')
}
order by ?p
It is not clear what you want. You must first know what exact related information dbpedia contains and how they are structured.
However, you can try discovering what types of relationships cat:Home_automation is involved in, thus, you may know better what you want.
I suggest starting by generic queries, to specify how cat:Home_automation occurs in dbpedia, then, you might be able to go more specific, and pose further queries.
A query to list triples where cat:Home_automation is an subject:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?p ?o WHERE {
cat:Home_automation ?p ?o
}
order by ?p
A query to list triples where cat:Home_automation is an object:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?s ?p WHERE {
?s ?p cat:Home_automation
}
order by ?p
check the results, see what is interesting for you, and then continue with further queries.

SPARQL - Return mutual objects of a list of subjects

How can i get all predicates + objects, which are shared by a list of subjects - without knowing anything about the predicates/objects of these subjects?
Let's look at this example query from Wikidata:
SELECT ?chancellor WHERE{
?chancellor wdt:P39 wd:Q4970706. #P39 = position held, Q4970706 = Chancellor of Germany
}
Link to this query.
This query returns all former chancellors of germany.
Now i want to return every predicate + object, which every chancellor has in common e.g. every of the subjects is an instance of human, is born in Germany and whatever.
I guess this is an easy one. However i have no idea.
This is a good one. Here's a near-hit:
prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
select ?p ?o (count(distinct ?chancellor) as ?cs) where {
?chancellor wdt:P39 wd:Q4970706.
?chancellor ?p ?o .
}
group by ?p ?o
order by desc(?cs)
Link to query
This takes all chancellors, and their properties and values. It counts the number of chancellors per prop/val.
By ordering that you can see the most common prop / vals at the top.
Now what you want is the only the results for all chancellors. We can get the number of chancellors in one query easily enough, and stick the two together:
prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
select ?p ?o where {
{
# Find number of chancellors
select (count(?chancellor) as ?num_chancellors) where {
?chancellor wdt:P39 wd:Q4970706
}
}
{
# Find number of chancellors per predicate / value
select ?p ?o (count(distinct ?chancellor) as ?chancellor_count) where {
?chancellor wdt:P39 wd:Q4970706.
?chancellor ?p ?o .
}
group by ?p ?o
}
# Choose results all chancellors share
filter (?num_chancellors = ?chancellor_count)
}
Link to query.
I think this does what you want. Not very pretty, I confess.
An interesting aspect of SPARQL and RDF is that you don't need to know anything about the data to query it. In your case I'd suggest adding the triple pattern ?chancellor ?p ?o . and select ?p and ?o. From there you can choose any property you're looking for. Be sure to use OPTIONAL if some of the ?chancellor matches don't have that property value.

Getting a list of American physicists from DBpedia using SPARQL

I want to query the American Physicsts and get the list of physicists. How can I do this?
The SPARQL you need would look like this ....
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT *
WHERE {
?s dcterms:subject category:American_physicists .
}
see results here
If you want the list with some extra predicates you need to join more triple patterns using the variable ?s. For instance, to retrieve the birthdate for each physicist ...
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/ontology/>
SELECT *
WHERE {
?s dcterms:subject category:American_physicists .
?s dbpedia:birthDate ?bithdate .
}
results here