'Query to get first 3 images in Wikidata page

I'm trying to query data from species' pages on Wikidata with the following query :

SELECT ?animal ?animalLabel ?iucncode ?photo WHERE {
    VALUES ?iucncode { "714" }
?animal wdt:P627 ?iucncode
OPTIONAL {
    ?animal wdt:P18 ?photo.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

See it working here

I'm changing the value of ?iucncode to get data about different species. With the current query I get as many results as there are images, for a single page. I would like to have a response with a single line for each page, with 3 columns of data for the first 3 images (if available). Is there a way to do that with SPARQL ?

For the species in the example above, instead of having 4 lines with these columns :

animal animalLabel iucncode photo

I would like to have a single line like so :

animal animalLabel iucncode photo_1 photo_2 photo_3


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source