Aggregate

GROUP_CONCAT

Combine multiple values into a single string. GROUP_CONCAT concatenates all values in a group with a separator, perfect for collecting related items together.

Understanding GROUP_CONCAT

GROUP_CONCAT is an aggregate function that joins multiple values into a single delimited string. It's useful when you want to see all related items in one row rather than multiple rows.

SyntaxDescription
GROUP_CONCAT(?var)Concatenate with space separator
GROUP_CONCAT(?var; SEPARATOR=", ")Concatenate with custom separator
GROUP_CONCAT(DISTINCT ?var)Concatenate unique values only

Basic GROUP_CONCAT Examples

Country Official Languages
Run ↗
SELECT ?country ?countryLabel (GROUP_CONCAT(DISTINCT ?langLabel; SEPARATOR=", ") AS ?languages)
WHERE {
  VALUES ?country { wd:Q39 wd:Q31 wd:Q183 wd:Q16 }
  ?country wdt:P37 ?lang .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?lang rdfs:label ?langLabel. ?country rdfs:label ?countryLabel. }
}
GROUP BY ?country ?countryLabel

This query lists the official languages of four multilingual countries: Switzerland (Q39), Belgium (Q31), Germany (Q183), and Canada (Q16). GROUP_CONCAT combines all languages into a comma-separated list for each country.

Author's Occupations
Run ↗
SELECT ?person ?personLabel (GROUP_CONCAT(DISTINCT ?occLabel; SEPARATOR=", ") AS ?occupations)
WHERE {
  VALUES ?person { wd:Q5879 wd:Q692 wd:Q7836 wd:Q1394 }
  ?person wdt:P106 ?occ .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?occ rdfs:label ?occLabel. ?person rdfs:label ?personLabel. }
}
GROUP BY ?person ?personLabel

Historical figures often had multiple occupations. This query shows all occupations for Goethe (Q5879), Shakespeare (Q692), Nietzsche (Q7836), and Voltaire (Q1394) in a single comma-separated list per person.

Different Separators

Borders with Semicolon Separator
Run ↗
SELECT ?country ?countryLabel (GROUP_CONCAT(DISTINCT ?borderLabel; SEPARATOR="; ") AS ?borders)
WHERE {
  VALUES ?country { wd:Q183 wd:Q142 wd:Q38 }
  ?country wdt:P47 ?border .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?border rdfs:label ?borderLabel. ?country rdfs:label ?countryLabel. }
}
GROUP BY ?country ?countryLabel

The SEPARATOR parameter allows any string delimiter. This example uses semicolons to list all countries that share borders with Germany (Q183), France (Q142), and Italy (Q38).

GROUP_CONCAT with COUNT

Countries with Their Cities
Run ↗
SELECT ?country ?countryLabel (COUNT(?city) AS ?cityCount)
  (GROUP_CONCAT(DISTINCT ?cityLabel; SEPARATOR=", ") AS ?cities)
WHERE {
  VALUES ?country { wd:Q347 wd:Q574 wd:Q414 }
  ?city wdt:P31 wd:Q515 ;
        wdt:P17 ?country ;
        wdt:P1082 ?pop .
  FILTER(?pop > 100000)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?city rdfs:label ?cityLabel. ?country rdfs:label ?countryLabel. }
}
GROUP BY ?country ?countryLabel
ORDER BY DESC(?cityCount)

Combining COUNT and GROUP_CONCAT lets you see both how many items exist and what they are. This query shows cities with over 100,000 people in Liechtenstein (Q347), East Timor (Q574), and Argentina (Q414).

Key Entities Used in Examples

EntityDescription
wd:Q39Switzerland
wd:Q31Belgium
wd:Q183Germany
wd:Q16Canada
wd:Q515City
wdt:P37Official language
wdt:P47Shares border with
wdt:P106Occupation
wdt:P17Country
wdt:P1082Population