Wikidata SPARQL filter properties by id - sparql

I have the following sparql:
#Cats
SELECT ?item ?itemLabel ?prop
WHERE
{
?item ?prop wd:Q7206.
filter not exists {?prop **FILTER** wdt:P921 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
# Helps get the label in your language, if not, then en language
}
I want to return relations excluding P921. What should I enter instead of FILTER?

Related

Select all statements using alternate identifier in Wikidata SPARQL

When I use a Q ID to do a query, I can get all the statements and identifiers -- EVERYTHING, e.g.
SELECT ?value ?valueLabel ?prop ?propLabel {
wd:Q112133367 ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
This query successfully returns about 30 rows of data.
However, that assumes that I know the Q ID in advance. I am trying to retrieve the same results, but using an external identifier, e.g. the IMDb ID because I don't know the Q ID in advance.
This query works to look up the Q ID given the IMDb external ID:
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P345 "tt13276352"
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
But this only returns a single row of data (the Q ID). I have tried adding some additional values to the query, but the query times out, e.g.
SELECT ?item ?itemLabel ?foo ?prop ?value
WHERE
{
?item wdt:P345 "tt13276352" .
?foo ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
I am stuck thinking about this too much from the point of view of a relational database so I don't see how to select all the same bits of data as when I use the Q ID.
The problem is that you have introduced an extra variable ?foo.
Re-use the ?item variable instead:
SELECT ?item ?itemLabel ?prop ?value
WHERE
{
?item wdt:P345 "tt13276352" .
?item ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Link to Wikidata Query Service for the above.

How to return null results in SPARQL?

I'm using the Wikidata SPARQL Query Service with the following query:
SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
VALUES ?item { wd:Q1 wd:Q367204 }
?item wdt:P31 ?class;
wdt:P18 ?project.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en". } }
link to the query
When running the query there is a result for Q1 because there is an image, but there is no result for Q367204 because there isn't an image.
My question: How can I get results for both Q1 and Q367204 regardless if there is an image available or not?
wd:Q367204 is missing from the results because "there isn't an image", but also because there isn't an "instance of" statement (P31). Therefore, you can get results for both instances by wrapping both of these in an OPTIONAL block, with just ?item and ?itemLabel for Q367204, and all variables for Q1:
SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
VALUES ?item { wd:Q1 wd:Q367204 }
OPTIONAL {
?item wdt:P31 ?class;
wdt:P18 ?project.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en". }
}

Get specific item from Wikidata with Sparql

I want to get the data of a specific wikidata item (e.g. Q1651322) with Sparql and then extract the image URL.
I tried this:
SELECT ?pic
WHERE
{
?item wdt:P18 ?pic .
FILTER(?item = 'Q1651322')
SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
}
LIMIT 1
and also this:
SELECT DISTINCT ?item WHERE {
?item ps:P1629 wd:Q1651322.
}
But these queries don't find records. How can I filter for a specific item by its ID?
SELECT ?pic
WHERE
{
?item wdt:P18 ?pic .
FILTER(?item = wd:Q1651322)
SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
}
LIMIT 1
This query finds the record. It is necessary to prefix the item ID with wd:.

Find people whose name start with a particular letter with sparql Wikidata?

This is my query but I don't get any results for records starting with "n"
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31/wdt:P279* wd:Q5.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
FILTER regex (?itemLabel, "^(n)").
}

Wikidata Query Service: lookup property label

How do I form the query to look up the English label of a property on a given entity.
This query provides the ID of P31:
SELECT ?item ?itemLabel ?instance_of WHERE {
?item wdt:P646 "/m/09c7w0".
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
OPTIONAL {
?item wdt:P31 ?instance_of.}
}
}
LIMIT 1
How can I get the label of P31 instead?
Also, is there a smart way to get the "best" value of P31, possibly the one with the lowest numeric ID? Some detailed entities have very noisy values. For example, for Q30, "United States" I would prefer to see "country" as my instance_of instead of "member state of the United Nations".
Thanks AKSW. label service applies to the property given a variable with the matching suffix Label:
SELECT ?item ?itemLabel ?instance_of ?instance_ofLabel WHERE {
?item wdt:P646 "/m/09c7w0".
OPTIONAL {
?item wdt:P31 ?instance_of.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 1