query: PREFIX wd: <http://www.wikidata.org/entity/>
  PREFIX wdt: <http://www.wikidata.org/prop/direct/>
  PREFIX wikibase: <http://wikiba.se/ontology#>
  PREFIX p: <http://www.wikidata.org/prop/>
  PREFIX v: <http://www.wikidata.org/prop/statement/>
  PREFIX q: <http://www.wikidata.org/prop/qualifier/>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  PREFIX ps: <http://www.wikidata.org/prop/statement/>
  PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
  
   #  Query to show England administrative hierarchy
   #   -- regions, ceremonial counties, administrative counties, and districts
   #  Compare [[:en:Subdivisions of England]]
  
      SELECT ?region ?regionLabel ?cerem ?ceremLabel ?adm_county ?adm_countyLabel 
              (GROUP_CONCAT(DISTINCT(?dist_label); separator="; ") AS ?dists)
      WHERE {
            ?region wdt:P131 wd:Q21 .      # England
            ?region wdt:P31  wd:Q48091 .   # region
          
            {?cerem  wdt:P131 ?region .   ?cerem  wdt:P31  wd:Q180673 }    # ceremonial county
            UNION
            {?region wdt:P31  wd:Q180673 .  BIND(?region AS ?cerem)}     # Greater London
            
            VALUES ?allowed_adm_county_type { wd:Q769628  wd:Q769603 wd:Q1136601 }
                   # metropolitan county, non-metropolitan county, unitary authority
            {  
               ?adm_county wdt:P131 ?cerem .
               ?adm_county wdt:P31  ?allowed_adm_county_type
            }  
            UNION
            {
               ?cerem wdt:P31 ?allowed_adm_county_type   
               BIND (?cerem AS ?adm_county)
            }
            UNION
            {?region wdt:P31  wd:Q180673 .  BIND(?cerem AS ?adm_county)}     # Greater London
                   
            VALUES ?allowed_district_type { wd:Q1187580  wd:Q1002812 wd:Q211690 }
            OPTIONAL {
              ?dist wdt:P131 ?adm_county .
              ?dist wdt:P31  ?allowed_district_type .
              ?dist rdfs:label ?dist_label filter (lang(?dist_label) = "en") .
            }
            OPTIONAL {
              ?adm_county wdt:P31  wd:Q1136601 .
              BIND (' (unitary) ' AS ?dist_label) 
            }
  
          
            FILTER (!(sameTerm(?adm_county, wd:Q1342914) && sameTerm(?region, wd:Q47994))) .   # North East Lincs
            FILTER (!(sameTerm(?adm_county, wd:Q175945) && sameTerm(?region, wd:Q47994))) .    # North Lincs
            FILTER (!(sameTerm(?adm_county, wd:Q21269047) && sameTerm(?region, wd:Q48063))) .  # Lincs council area
            FILTER (!(sameTerm(?adm_county, wd:Q171866) && sameTerm(?region, wd:Q48063))) .    # Middlesbrough
            FILTER (!(sameTerm(?adm_county, wd:Q1434448) && sameTerm(?region, wd:Q48063))) .   # Redcar & Cleveland
            FILTER (!(sameTerm(?adm_county, wd:Q894094) && sameTerm(?region, wd:Q48063))) .    # Stockton
            FILTER (!(sameTerm(?adm_county, wd:Q21241814) && sameTerm(?region, wd:Q47983))) .  # North Yorkshire
            FILTER (!(sameTerm(?adm_county, wd:Q42462) && sameTerm(?region, wd:Q47983))) .     # York
          
            SERVICE wikibase:label {
                 bd:serviceParam wikibase:language "en" .
            }
      } GROUP BY ?region ?regionLabel ?cerem ?ceremLabel ?adm_county ?adm_countyLabel
      ORDER BY ?regionLabel ?ceremLabel ?adm_countyLabel