Search code examples
sparqlwikidata

SPARQL: how do you also return the entity and not only its propery?


I have the following query

SELECT ?animal ?timePregnant WHERE {
  wd:Q15083 wdt:P3063 ?timePregnant .
  SERVICE wikibase:label
  {bd:serviceParam wikibase:language "en"}
}

How do I make sure that it returns giraffe and 457 days? At the moment it is only returning an empty label for animal and then 457 for the days.

SELECT ?animal ?timePregnant WHERE {
  ?animal wdt:P3063 ?timePregnant .
  SERVICE wikibase:label
  {bd:serviceParam wikibase:language "en"}
}

I thought about trying something like this but for this it returns all entities. I need to add a second constraint but I do not know how I set the constraint that ?animal is Q15083.

What is the constraint needed?


Solution

  • This query returns all the animals IDs, their labels and their gestation periods:

    SELECT ?animal ?animalLabel ?timePregnant WHERE{
      ?animal wdt:P3063 ?timePregnant .
      SERVICE wikibase:label
      {bd:serviceParam wikibase:language "en"}
    }
    

    If you want to limit the set of animals, you can use VALUES ?animal { wd:Q15083 } in this way:

    SELECT ?animal ?animalLabel ?timePregnant WHERE{
      VALUES ?animal { wd:Q15083 }
      ?animal wdt:P3063 ?timePregnant .
      SERVICE wikibase:label
      {bd:serviceParam wikibase:language "en"}
    }