PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?commune ?communeLabel WHERE {
  ?commune wdt:P31 wd:Q17318027 .
  {
    ?commune wdt:P17 wd:Q1028 .
  } UNION {
    ?commune wdt:P131 * ?admin .
    ?admin wdt:P17 wd:Q1028 .
  }
  OPTIONAL {
    ?commune rdfs:label ?communeLabel .
    FILTER ( LANG( ?communeLabel ) = "en" )
  }
}
ORDER BY ?communeLabel

Using entities:
- Morocco (wd:Q1028), also known as al-Maġrib, Kingdom of Morocco, Lmaġrib, ma, MAR, etc.: is instance of sovereign state / is instance of constitutional monarchy / is instance of Mediterranean country / is instance of country / country in North Africa / country in North Africa / country in North Africa / sovereign state in North Africa
- rural commune of Morocco (wd:Q17318027), also known as rural commune: is subclass of rural settlement / is subclass of fourth-level administrative country subdivision / is subclass of fourth-level administrative division / is subclass of commune of Morocco / is subclass of administrative territorial entity of Morocco / administrative division of Morocco

Using properties:
- located in the administrative territorial entity (wd:P131 as wdt), also known as administrative territorial entity, administrative territory, city, happens in, in, etc.: is subproperty of location / is subproperty of part of / the item is located on the territory of the following administrative entity / the item is located on the territory of the following administrative entity / the item is located on the territory of the following ... the item falls only partially into the administrative entity. / the item is located on the territory of the following ... the item falls only partially into the administrative entity.
- country (wd:P17 as wdt), also known as host country, land, sovereign state, state, P17: is subproperty of located in the administrative territorial entity / sovereign state that this item is in (not to be used for human beings) / sovereign state that this item is in (not to be used for human beings) / sovereign state that this item is in (not to be used for human beings) / sovereign state that this item is in (not to be used for human beings)
- instance of (wd:P31 as wdt), also known as example of, has type, is a, is an, is of type, etc.: that class of which this subject is a particular example and ... a subclass of mountain (and an instance of volcanic landform) / type to which this subject corresponds/belongs. Different ... and the volcano is a type (instance) of volcanic landform

Got 1,281 rows and 2 columns, showing the first 5 and last 5 rows below:
| commune                            | communeLabel                  |
| ---------------------------------- | ----------------------------- |
| wd:Q12181212                       |                               |
| wd:Q27533449                       |                               |
| wd:Q6539156                        |                               |
| Aarab Sebbah Gheris (wd:Q20384963) | Aarab Sebbah Gheris (lang:en) |
| Aarab Sebbah Ziz (wd:Q20384972)    | Aarab Sebbah Ziz (lang:en)    |
| ...                                | ...                           |
| Znada (wd:Q20421090)               | Znada (lang:en)               |
| Zouada (wd:Q12214765)              | Zouada (lang:en)              |
| Zoumi (wd:Q8074648)                | Zoumi (lang:en)               |
| Zrarda (wd:Q8074757)               | Zrarda (lang:en)              |
| Zrizer (wd:Q12188091)              | Zrizer (lang:en)              |

Answer: I ran the query above on Wikidata. It returns 1,281 items (IRIs) that are instances of "rural commune of Morocco" (wd:Q17318027) and that are located in Morocco (wd:Q1028), either directly or via an administrative parent. The query returns each commune's Wikidata IRI and its English label (when available). You can run the SPARQL query to retrieve the full list of rural communes and their IRIs/labels.

I ran the query above on Wikidata. It returns 1,281 items (IRIs) that are instances of "rural commune of Morocco" (wd:Q17318027) and that are located in Morocco (wd:Q1028), either directly or via an administrative parent. The query returns each commune's Wikidata IRI and its English label (when available). You can run the SPARQL query to retrieve the full list of rural communes and their IRIs/labels.

SPARQL query over wikidata:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?commune ?communeLabel WHERE {
  ?commune wdt:P31 wd:Q17318027 .
  {
    ?commune wdt:P17 wd:Q1028 .
  } UNION {
    ?commune wdt:P131 * ?admin .
    ?admin wdt:P17 wd:Q1028 .
  }
  OPTIONAL {
    ?commune rdfs:label ?communeLabel .
    FILTER ( LANG( ?communeLabel ) = "en" )
  }
}
ORDER BY ?communeLabel

Using entities:
- Morocco (wd:Q1028), also known as al-Maġrib, Kingdom of Morocco, Lmaġrib, ma, MAR, etc.: is instance of sovereign state / is instance of constitutional monarchy / is instance of Mediterranean country / is instance of country / country in North Africa / country in North Africa / country in North Africa / sovereign state in North Africa
- rural commune of Morocco (wd:Q17318027), also known as rural commune: is subclass of rural settlement / is subclass of fourth-level administrative country subdivision / is subclass of fourth-level administrative division / is subclass of commune of Morocco / is subclass of administrative territorial entity of Morocco / administrative division of Morocco

Using properties:
- located in the administrative territorial entity (wd:P131 as wdt), also known as administrative territorial entity, administrative territory, city, happens in, in, etc.: is subproperty of location / is subproperty of part of / the item is located on the territory of the following administrative entity / the item is located on the territory of the following administrative entity / the item is located on the territory of the following ... the item falls only partially into the administrative entity. / the item is located on the territory of the following ... the item falls only partially into the administrative entity.
- country (wd:P17 as wdt), also known as host country, land, sovereign state, state, P17: is subproperty of located in the administrative territorial entity / sovereign state that this item is in (not to be used for human beings) / sovereign state that this item is in (not to be used for human beings) / sovereign state that this item is in (not to be used for human beings) / sovereign state that this item is in (not to be used for human beings)
- instance of (wd:P31 as wdt), also known as example of, has type, is a, is an, is of type, etc.: that class of which this subject is a particular example and ... a subclass of mountain (and an instance of volcanic landform) / type to which this subject corresponds/belongs. Different ... and the volcano is a type (instance) of volcanic landform

Execution result:
Got 1,281 rows and 2 columns, showing the first 5 and last 5 rows below:
| commune                            | communeLabel                  |
| ---------------------------------- | ----------------------------- |
| wd:Q12181212                       |                               |
| wd:Q27533449                       |                               |
| wd:Q6539156                        |                               |
| Aarab Sebbah Gheris (wd:Q20384963) | Aarab Sebbah Gheris (lang:en) |
| Aarab Sebbah Ziz (wd:Q20384972)    | Aarab Sebbah Ziz (lang:en)    |
| ...                                | ...                           |
| Znada (wd:Q20421090)               | Znada (lang:en)               |
| Zouada (wd:Q12214765)              | Zouada (lang:en)              |
| Zoumi (wd:Q8074648)                | Zoumi (lang:en)               |
| Zrarda (wd:Q8074757)               | Zrarda (lang:en)              |
| Zrizer (wd:Q12188091)              | Zrizer (lang:en)              |

[System]
You are a question answering assistant. Your job is to generate a SPARQL query to answer a given user question.

You should follow a step-by-step approach to generate the SPARQL query:
1. Determine possible entities and properties implied by the user question.
2. Search for the entities and properties in the knowledge graphs. Where applicable, constrain the searches with already identified entities and properties.
3. Gradually build up the SPARQL query using the identified entities and properties. Start with simple queries and add more complexity as needed. Execute intermediate queries to get feedback and to verify your assumptions. You may need to refine or rethink your current plan based on the query results and go back to step 2 if needed, possibly multiple times.
4. Use the answer or cancel function to finalize your answer and stop the generation process.

Available knowledge graphs:
- wikidata at http://localhost:7001 with prefix index for entities and similarity index for properties, and notes:
  - P106 = occupation. Use wdt:P106 to get occupations and fetch English labels with rdfs:label + FILTER(LANG(?label)="en"). When returning human occupations confirm the subject is human (wdt:P31 wd:Q5) to avoid non-human items.
  - Population (P1082) and tabular (P4179): use wdt:P1082 for quick/direct values; for time-series/point-in-time use p:P1082 + ps:P1082 + pq:P585 and ORDER BY pq:P585 DESC. Inspect statement qualifiers (e.g., 'applies to part') and prov:wasDerivedFrom references (pr:P854 etc.) to detect city-proper vs metro/prefecture scopes before using values.
  - Common person properties: P31 (instance of human Q5), P106 (occupation), P569 (date of birth), P19 (place of birth), P27 (country of citizenship), P21 (sex/gender), P166 (award received). Use wdt: for direct values when possible.
  - P17 (country) can be incomplete/misleading for places (historical/overseas tags). To get modern cities in a country combine: instance-of city (P31=Q515) + wdt:P17 OR use wdt:P131* to find an admin parent whose wdt:P17 = the country. Also cross-check coordinates (P625) or admin hierarchy to spot outliers.
  - When filtering by date of birth (P569) use xsd:dateTime comparisons (e.g. >= "1900-01-01T00:00:00Z"^^xsd:dateTime). Be aware many items have year-only or imprecise dates and coverage is biased toward notable/public figures (sportspeople, politicians, artists).
  - To retrieve qualifiers (e.g., award year) use the statement/qualifier pattern: p:Pxxx / ps:Pxxx / pq:P585. Use wdt:Pxxx for normalized/direct values when you don't need qualifiers. Example: p:P166 + pq:P585 gives award year.
  - To get a person's occupations: use wdt:P106 for direct/normalized values and rdfs:label for human-readable names (FILTER(LANG(...))). Example: wd:Q... wdt:P106 ?occ . ?occ rdfs:label ?occLabel . FILTER(LANG(?occLabel)="en").
  - P166 = award received. Use wdt:P166 for normalized/direct values when you only need award items. To get award years or qualifiers use p:P166 / ps:P166 / pq:P585. Many statements lack pq:P585 — use OPTIONAL { ?stmt pq:P585 ?date } and ORDER BY DESC(?date). When listing award winners, filter ?person wdt:P31 wd:Q5 to ensure humans.
  - Population time-series pattern: wd:Q... p:P1082 ?stmt . ?stmt ps:P1082 ?pop . OPTIONAL { ?stmt pq:P585 ?date } . ORDER BY DESC(?date). pq:P585 is the point-in-time qualifier. IMPORTANT: time-series may mix scopes (city proper vs metro) — e.g., Tokyo has a 2016 entry ~38,140,000 likely metro vs smaller city figures. Inspect qualifiers/references for scope before use.
  - Use wdt:P131* to traverse the admin hierarchy and compare wdt:P17 on the place vs an admin parent to detect historical/colonial country assignments. When mismatches appear, inspect the admin parent's instance-of (P31), labels, qualifiers and references to interpret the historical scope.
  - wdt:P106 often yields multiple occupations for a person. Use GROUP_CONCAT(DISTINCT ?occLabel; separator=", ") or return multiple rows. Always fetch labels with rdfs:label + FILTER(LANG(?label)="en") to get readable occupation names.
  - When a search_entity returns multiple hits (person vs sculpture vs memorial), confirm you selected the human/statements by checking wdt:P31 (instance of) and relevant wdt: properties (e.g., wdt:P106). This avoids using a non-human QID by mistake.
  - search_autocomplete can be constrained with a SPARQL pattern containing ?search (e.g., SELECT * WHERE { ?search wdt:P31 wd:Q5 }). Use constraints to focus results (e.g., humans) and avoid ambiguous fallbacks; if it returns no results, fallback to search_entity and verify wdt:P31.
  - Use search_property_of_entity to find relevant properties for an entity (e.g., 'award' -> P166) and search_object_of_property to discover candidate objects (e.g., 'physicist' for P106). This reduces guessing P/Q ids and surfaces qualifiers.
  - Inspect statement references and qualifiers: use prov:wasDerivedFrom on p: statements to find sources, and check qualifiers (e.g., 'applies to part' or other scope qualifiers) to disambiguate city vs metro vs admin area. Reference labels can be empty; inspect reference node properties for URLs or publication dates.

Index types used for entities and properties:
- Prefix index: Retrieves items by overlap between query keywords and item words. The query keywords can match item words exactly or as prefixes.
- Similarity index: Retrieves items by cosine similarity between their embeddings and the query embedding.

General notes across knowledge graphs:
- Use rdfs:label + FILTER(LANG(?label)="en") for labels; avoid SERVICE wikibase:label. Use wdt:Pxxx for normalized/direct values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access statement qualifiers (e.g., award year via pq:P585). When dates matter, prefer xsd:dateTime and watch for year-only precision.
- Practical query patterns: use rdfs:label + FILTER(LANG(...)) for labels. Use wdt:Pxxx for direct/normalized values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access qualifiers (dates via pq:P585). For dates use xsd:dateTime and expect year-only precision. For places: combine P17 with P131 or coordinate (P625) filters to avoid historical/overseas mismatches.
- Use OPTIONAL for pq: qualifiers (e.g., pq:P585), sort by qualifier (ORDER BY pq:P585 DESC) to prefer recent values. Use GROUP_CONCAT to combine multiple labels/values in one row. Watch for typed literals (xsd:dateTime, xsd:decimal) when filtering or sorting.
- Before querying, use search_entity to resolve human-friendly names to wd:Q identifiers and verify instance-of (e.g., check wdt:P31 = wd:Q5 for humans) to avoid ambiguous matches. Prefer the top match but programmatically confirm P31 before using its data.
- Use search_property to confirm property identifiers (e.g., "country" -> P17) before embedding them in queries. This prevents mistakes from assuming an ID and helps pick the best property if multiple choices exist.
- search_autocomplete can fall back to an unconstrained search or error when there are too many candidates. If this happens (e.g., "more than the maximum supported number of subject candidates"), retry with narrower constraints such as instance-of (wdt:P31), additional type filters, or short prefixes to reduce the candidate set; otherwise fallback to search_entity and verify wdt:P31.
- Use list(...) to inspect all triples on an entity for exploration, but expect large results for notable items. Prefer wdt: properties or targeted p:/ps:/pq: patterns in SPARQL when you need normalized values or qualifiers, respectively.
- Before embedding property or item IDs in SPARQL, use search_property, search_property_of_entity, and search_object_of_property to discover the appropriate P/PID or object QIDs. This reduces errors from guessing IDs and surfaces better property alternatives and qualifiers.
- search_constrained and search_autocomplete can return no results for constrained queries or when the index is large. If they fail, fallback to search_entity + verify P31 (or use search_property_of_entity). Constrain with instance-of (wdt:P31) or other properties in the SPARQL to improve matches.
- When selecting awards by year, prefer xsd:dateTime range filters (e.g., >= "2001-01-01T00:00:00Z"^^xsd:dateTime && < "2002-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for portability and precision.
- When selecting award winners via wdt:P166, filter ?person wdt:P31 wd:Q5 to ensure humans — fictional characters or non-humans may have award claims. If you need award years use p:P166 / ps:P166 / pq:P585 and OPTIONAL { ?stmt pq:P585 ?year } to retrieve qualifier years.
- Use wdt:P625 (coordinates) together with wdt:P131* and wdt:P17 checks to validate a place's modern country or to filter by geography. When country labels mismatch historically, inspect admin parents and coordinates to resolve whether the item refers to historical/colonial scope or current boundaries.
- Prefer wdt:Pxxx for normalized/direct values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access qualifiers (e.g., pq:P585 for award year). Always fetch labels with rdfs:label + FILTER(LANG(?label) = "en").
- If search_autocomplete returns too many candidates or errors, retry with constraints (e.g., SELECT * WHERE { ?search wdt:P31 wd:Q5 }) or narrower query text. If still failing, fallback to search_entity and verify wdt:P31 for type checks.
- For award-year queries use p:P166 / ps:P166 / pq:P585 to access award statements and year qualifiers. Filter by xsd:dateTime ranges (e.g., >= "2020-01-01T00:00:00Z"^^xsd:dateTime && < "2021-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for robustness.
- Use search_property_of_entity to surface relevant properties for an entity (e.g., 'birth' -> P569/P19). list(...) can inspect triples but may return large results for notable items—prefer targeted wdt: patterns (wdt:Pxxx) or p:/ps:/pq: patterns when you need qualifiers.

SPARQL prefixes for use in function calls:
- bd: <http://www.bigdata.com/rdf#
- cc: <http://creativecommons.org/ns#
- data: <https://www.wikidata.org/wiki/Special:EntityData/
- dc: <http://purl.org/dc/elements/1.1/
- dct: <http://purl.org/dc/terms/
- foaf: <http://xmlns.com/foaf/0.1/
- geo: <http://www.opengis.net/ont/geosparql#
- geof: <http://www.opengis.net/def/function/geosparql/
- gn: <http://www.geonames.org/ontology#
- hint: <http://www.bigdata.com/queryHints#
- imdb: <https://www.imdb.com/
- math: <http://www.w3.org/2005/xpath-functions/math#
- ontolex: <http://www.w3.org/ns/lemon/ontolex#
- owl: <http://www.w3.org/2002/07/owl#
- p: <http://www.wikidata.org/prop/
- pq: <http://www.wikidata.org/prop/qualifier/
- pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/
- pqv: <http://www.wikidata.org/prop/qualifier/value/
- pr: <http://www.wikidata.org/prop/reference/
- prn: <http://www.wikidata.org/prop/reference/value-normalized/
- prov: <http://www.w3.org/ns/prov#
- prv: <http://www.wikidata.org/prop/reference/value/
- ps: <http://www.wikidata.org/prop/statement/
- psn: <http://www.wikidata.org/prop/statement/value-normalized/
- psv: <http://www.wikidata.org/prop/statement/value/
- qb: <http://purl.org/linked-data/cube#
- qfn: <http://qlever.cs.uni-freiburg.de/function#
- ql: <http://qlever.cs.uni-freiburg.de/builtin-functions/
- qlss: <https://qlever.cs.uni-freiburg.de/spatialSearch/
- rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
- rdfs: <http://www.w3.org/2000/01/rdf-schema#
- ref: <http://www.wikidata.org/reference/
- s: <http://www.wikidata.org/entity/statement/
- schema: <http://schema.org/
- skos: <http://www.w3.org/2004/02/skos/core#
- v: <http://www.wikidata.org/value/
- void: <http://rdfs.org/ns/void#
- wd: <http://www.wikidata.org/entity/
- wdno: <http://www.wikidata.org/prop/novalue/
- wdt: <http://www.wikidata.org/prop/direct/
- wdtn: <http://www.wikidata.org/prop/direct-normalized/
- wikibase: <http://wikiba.se/ontology#
- xsd: <http://www.w3.org/2001/XMLSchema#

Additional rules to follow:
- Explain your thought process before and after each step and function call.
- Do not just use or make up entity or property identifiers without verifying their existence in the knowledge graphs first.
- Do not ask the user for clarifications without first attempting to complete the task. If the task input is incomplete or ambiguous, try to make reasonable assumptions and document them.
- Do not use "SERVICE wikibase:label { bd:serviceParam wikibase:language ..." in SPARQL queries. It is not SPARQL standard and unsupported by the used QLever SPARQL endpoints. Use rdfs:label or similar properties to get labels instead.
- Always execute your final SPARQL query before giving an answer to make sure it returns the expected results.
- The SPARQL query should always return the actual identifiers / IRIs of the items in its result. It additionally may return labels or other human-readable information, but they are optional and should be put within optional clauses unless explicitly requested by the user.
- Do not stop early if there are still obvious improvements to be made to the SPARQL query. For example, keep refining your SPARQL query if its result contains irrelevant items or is missing items you expected.
- Do not perform additional computation (e.g. filtering, sorting, calculations) on the result of the SPARQL query to determine the answer. All computation should be done solely within SPARQL.
- For questions with a "True" or "False" answer the SPARQL query should be an ASK query.

[User] What are the rural communes of Morocco?

[Assistant]
Plan:
1. Search Wikidata for an item that represents the class "rural commune" (or "commune rurale") and verify its existence.
2. Find how Morocco is represented (country wd:Q... for Morocco) and confirm.
3. Find a property that indicates "located in the administrative territorial entity" (P131) or "country" (P17) and a property or instance-of (P31) pattern that marks an item as a "rural commune".
4. Build a SPARQL query that returns all items that are instances of "rural commune" and are located in Morocco (directly or via admin hierarchy).
I'll now search for the "rural commune" item in Wikidata.

Question: rural commune

Top 10 entity alternatives:
1. rural commune of Morocco (wd:Q17318027, matched via rural commune), also known as rural commune: is subclass of fourth-level administrative country subdivision / is subclass of administrative territorial entity of Morocco / is subclass of fourth-level administrative division / administrative division of Morocco / is subclass of commune of Morocco / is subclass of rural settlement
2. rural commune of Vietnam (wd:Q2389082), also known as xa, xã, Xã: is subclass of third-level administrative country subdivision / is subclass of commune-level subdivision of Vietnam / is subclass of third-level administrative division / is subclass of human settlement / type of subdivision of Vietnam
3. commune rurale de Nojaï-Iourt (wd:Q24636444): human settlement in Nozhay-Yurtovsky District, Chechen Republic, Russia / is instance of rural settlement in Russia
4. Nostoc commune subsp. commune (wd:Q49549856, matched via Nostoc commune commune), also known as Nostoc commune commune: subspecies of Cyanobacteria / is instance of taxon
5. Bourg-en-Bresse (wd:Q133069, matched via Commune-Neuve (ou Commune-Nouvelle)), also known as Bledvin, Bourg-Épuré, Bourg-Régénéré, Commune-Affranchie, Commune-Neuve (ou Commune-Nouvelle), etc.: is instance of commune of France / commune in Ain, France
6. Paris Commune (wd:Q133132), also known as Commune: is instance of administrative territorial entity / Revolutionary city council of Paris in 1871 / is instance of social experiment / is instance of organization / is instance of organisation / is instance of group action / is instance of insurgency / is instance of Insurgency / is instance of commune
7. commune of France (wd:Q484170, matched via commune), also known as commune, commune (France): is subclass of fourth-level administrative country subdivision / is instance of type of French administrative division / is subclass of fourth-level administrative division / is subclass of fifth-level administrative division / is subclass of territorial collectivity of France / France territorial subdivision for municipalities / is subclass of electoral unit / is subclass of constituency / is subclass of municipality / is subclass of commune / is subclass of riding / is subclass of LAU 2
8. rural area (wd:Q175185), also known as countryside, regional area, rural areas, rural place, rural region: geographic area that is located outside towns and cities / is subclass of geographic location / is subclass of geographical region / is subclass of geographic region / is subclass of human settlement
9. Salamanca (wd:Q56134, matched via Salamanca Commune), also known as Salamanca, Chile, Salamanca Commune: is instance of administrative territorial entity / is instance of Communes of Chile / is instance of commune of Chile / is instance of city in Chile / commune of Chile / commune of Chile
10. Saint-Pierre (wd:Q185678, matched via Saint-Pierre Commune), also known as Saint Pierre, Saint Pierre Commune, Saint-Pierre Commune, Saint Pierre en l'Ile, Saint-Pierre-en-l'Ile, etc.: commune in Saint-Pierre-et-Miquelon, France / is instance of human settlement / is instance of Commune

[Assistant]
I will look up the Wikidata item for Morocco to get its Q-id so I can constrain communes to that country. Then I'll build a SPARQL query that finds items with instance-of = "rural commune of Morocco" (wd:Q17318027) and that are located in Morocco either directly (P17) or via an administrative parent (P131*). I'll include English labels. Now I will search Wikidata for "Morocco".

Question: Morocco

Top 10 entity alternatives:
1. Morocco (wd:Q1028), also known as al-Maġrib, Kingdom of Morocco, Lmaġrib, ma, MAR, etc.: is instance of constitutional monarchy / is instance of Mediterranean country / sovereign state in North Africa / is instance of sovereign state / country in North Africa / country in North Africa / country in North Africa / is instance of country
2. Morocco (wd:Q577754): 1930 film by Josef von Sternberg and Henry Hathaway / is instance of film
3. Morocco (wd:Q2374422), also known as Morocco, IN, Morocco, Indiana: is instance of town in the United States / town in Indiana, United States
4. Morocco leather (wd:Q2118555, matched via Morocco), also known as Levant, Morocco: soft, pliable leather used for bookbinding, wallets, linings for luggage, gloves and shoes / is subclass of leather
5. Morocco at the 1960 Summer Olympics (wd:Q2149869, matched via Morocco), also known as 1960 Summer Olympics, Morocco: is instance of Olympic delegation / sporting event delegation
6. Sharifian Empire (wd:Q12183875, matched via Morocco), also known as Morocco: is instance of historical country / historical name given to Morocco
7. Exit Marrakech (wd:Q14949869, matched via Morocco), also known as Morocco: 2013 film by Caroline Link / is instance of film
8. Morocco titular see (wd:Q3708053, matched via Morocco), also known as Marocco, Marruecos titular see, Morocco, Titular see of Marocco: is instance of Titular see / is instance of titular see / is instance of diocese / titular see
9. Morocco (wd:Q6913141), also known as Morocco, West Virginia, Morocco, WV: is instance of unincorporated community in the United States / unincorporated community in Clay County, West Virginia
10. Morocco (wd:Q126171591): scientific article published in 1970 / is instance of scholarly article / is instance of academic article

[Assistant]
Now I'll run a SPARQL SELECT query that finds all items that are instances of "rural commune of Morocco" (wd:Q17318027) and that are in Morocco either directly (P17) or via administrative parent (P131*). The query will return the item IRIs and their English labels (if available). I'll execute it to verify results.

execute

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?commune ?communeLabel WHERE {
  ?commune wdt:P31 wd:Q17318027 .
  {
    ?commune wdt:P17 wd:Q1028 .
  } UNION {
    ?commune wdt:P131* ?admin .
    ?admin wdt:P17 wd:Q1028 .
  }
  OPTIONAL { ?commune rdfs:label ?communeLabel . FILTER(LANG(?communeLabel) = "en") }
}
ORDER BY ?communeLabel

Got 1,281 rows and 2 columns, showing the first 5 and last 5 rows below:
| commune                            | communeLabel                  |
| ---------------------------------- | ----------------------------- |
| wd:Q12181212                       |                               |
| wd:Q27533449                       |                               |
| wd:Q6539156                        |                               |
| Aarab Sebbah Gheris (wd:Q20384963) | Aarab Sebbah Gheris (lang:en) |
| Aarab Sebbah Ziz (wd:Q20384972)    | Aarab Sebbah Ziz (lang:en)    |
| ...                                | ...                           |
| Znada (wd:Q20421090)               | Znada (lang:en)               |
| Zouada (wd:Q12214765)              | Zouada (lang:en)              |
| Zoumi (wd:Q8074648)                | Zoumi (lang:en)               |
| Zrarda (wd:Q8074757)               | Zrarda (lang:en)              |
| Zrizer (wd:Q12188091)              | Zrizer (lang:en)              |

[Assistant]
Final SPARQL query (already executed) and a short answer describing the result. Now sending via the answer function.

answer

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?commune ?communeLabel WHERE {
  ?commune wdt:P31 wd:Q17318027 .
  {
    ?commune wdt:P17 wd:Q1028 .
  } UNION {
    ?commune wdt:P131* ?admin .
    ?admin wdt:P17 wd:Q1028 .
  }
  OPTIONAL { ?commune rdfs:label ?communeLabel . FILTER(LANG(?communeLabel) = "en") }
}
ORDER BY ?communeLabel

Answer: I ran the query above on Wikidata. It returns 1,281 items (IRIs) that are instances of "rural commune of Morocco" (wd:Q17318027) and that are located in Morocco (wd:Q1028), either directly or via an administrative parent. The query returns each commune's Wikidata IRI and its English label (when available). You can run the SPARQL query to retrieve the full list of rural communes and their IRIs/labels.


PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?municipality ?municipalityLabel ?website ?coord ?state ?stateLabel WHERE {
  ?municipality wdt:P31 wd:Q42744322 .
  OPTIONAL {
    ?municipality rdfs:label ?municipalityLabel .
    FILTER ( LANG( ?municipalityLabel ) = "en" )
  }
  OPTIONAL {
    ?municipality wdt:P856 ?website
  }
  OPTIONAL {
    ?municipality wdt:P625 ?coord
  }
  OPTIONAL {
    ?municipality wdt:P131 + ?state .
    ?state wdt:P31 / wdt:P279 * wd:Q1221156 .
    OPTIONAL {
      ?state rdfs:label ?stateLabel .
      FILTER ( LANG( ?stateLabel ) = "en" )
    }
  }
}
ORDER BY ?municipalityLabel

Using entities:
- federated state of Germany (wd:Q1221156), also known as BL, Bundesland, German federal state, Land, state of Germany, etc.: is instance of designation for an administrative territorial entity of a single country / is subclass of federated state / is subclass of Federated state / is subclass of first-level administrative division / is subclass of administrative divisions of Germany / is subclass of NUTS 1 statistical territorial entity / administrative division of the Federal Republic of Germany
- urban municipality in Germany (wd:Q42744322), also known as city municipality of Germany, city of Germany: is subclass of place with town rights and privileges / is subclass of municipality in Germany / is subclass of city / municipality with town privileges in Germany (may e.g. legally prepend "Stadt" to its name)

Using properties:
- located in the administrative territorial entity (wd:P131 as wdt), also known as administrative territorial entity, administrative territory, city, happens in, in, etc.: is subproperty of location / is subproperty of part of / the item is located on the territory of the following administrative entity / the item is located on the territory of the following administrative entity / the item is located on the territory of the following ... the item falls only partially into the administrative entity. / the item is located on the territory of the following ... the item falls only partially into the administrative entity.
- subclass of (wd:P279 as wdt), also known as all members of this class also belong to, category of, form of, has superclass, is a class of, etc.: this item is a subclass (subset) of that item; ALL instances ... of volcanic landform); Everest is an instance of mountain / this item is a subclass (subset) of that item; all instances ... instance of volcanic landform); K2 is an instance of mountain / this item is a subclass (subset) of that item; all instances ... instance of volcanic landform); K2 is an instance of mountain / this item is a subclass (subset) of that item; all instances ... instance of volcanic landform); K2 is an instance of mountain
- instance of (wd:P31 as wdt), also known as example of, has type, is a, is an, is of type, etc.: that class of which this subject is a particular example and ... a subclass of mountain (and an instance of volcanic landform) / type to which this subject corresponds/belongs. Different ... and the volcano is a type (instance) of volcanic landform
- coordinate location (wd:P625 as wdt), also known as co-ordinate location, co-ordinates, coordinates, co-ords, coords, etc.: coordinates of the subject / coordinates of the subject / geocoordinates of the subject. For Earth, please note that only WGS84 coordinating system is supported at the moment
- official website (wd:P856 as wdt), also known as home page, homepage, home page (official), homepage (official), official homepage, etc.: is subproperty of URL / URL of the official page of an item (current or former). ... see the "Hijacked or dead websites" section of the Talk page

Got 2,093 rows and 6 columns, showing the first 5 and last 5 rows below:
| municipality             | municipalityLabel     | website                                  | coord                                       | state                             | stateLabel                       |
| ------------------------ | --------------------- | ---------------------------------------- | ------------------------------------------- | --------------------------------- | -------------------------------- |
| wd:Q3865                 |                       | <https://www.villingen-schwenningen.de/> | POINT(8.492962 48.063152) (geo:wktLiteral)  | Baden-Württemberg (wd:Q985)       | Baden-Württemberg (lang:en)      |
| Aach (wd:Q62158)         | Aach (lang:en)        | <http://www.aach.de/>                    | POINT(8.851667 47.845556) (geo:wktLiteral)  | Baden-Württemberg (wd:Q985)       | Baden-Württemberg (lang:en)      |
| Aachen (wd:Q1017)        | Aachen (lang:en)      | <https://www.aachen.de/>                 | POINT(6.083788 50.776207) (geo:wktLiteral)  | North Rhine-Westphalia (wd:Q1198) | North Rhine-Westphalia (lang:en) |
| Abenberg (wd:Q261822)    | Abenberg (lang:en)    | <https://www.abenberg.de/>               | POINT(10.961944 49.240555) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| Abensberg (wd:Q255698)   | Abensberg (lang:en)   | <https://www.abensberg.de/>              | POINT(11.850130 48.816720) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| ...                      | ...                   | ...                                      | ...                                         | ...                               | ...                              |
| Zwenkau (wd:Q10779)      | Zwenkau (lang:en)     | <https://www.zwenkau.de/>                | POINT(12.324167 51.217500) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |
| Zwickau (wd:Q3778)       | Zwickau (lang:en)     | <https://www.zwickau.de/>                | POINT(12.496111 50.718889) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |
| Zwiesel (wd:Q163091)     | Zwiesel (lang:en)     | <http://www.zwiesel.de>                  | POINT(13.233333 49.016667) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| Zwingenberg (wd:Q244821) | Zwingenberg (lang:en) | <https://zwingenberg.de/>                | POINT(8.613889 49.722500) (geo:wktLiteral)  | Hesse (wd:Q1199)                  | Hesse (lang:en)                  |
| Zwönitz (wd:Q58002)      | Zwönitz (lang:en)     | <https://www.zwoenitz.de/>               | POINT(12.813333 50.630278) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |

Answer:
I executed the SPARQL query above on Wikidata. It returns items that are instances of "urban municipality in Germany" (wd:Q42744322). For each municipality it returns:
- ?municipality — the Wikidata item IRI of the municipality
- ?municipalityLabel — the English label (when available)
- ?website — official website (P856), if available
- ?coord — coordinates (P625), if available (as a geo:wktLiteral)
- ?state — the administrative parent that is a German federated state (walking the P131 chain)
- ?stateLabel — English label of the federated state (when available)

The query returned 2,093 rows (i.e., 2,093 urban municipalities matched in Wikidata). If you want, I can:
- Export the full result table (CSV/JSON)
- Filter the list to a particular federated state
- Include German labels or additional properties (e.g., population)

Which output format would you prefer for the full list?

I executed the SPARQL query above on Wikidata. It returns items that are instances of "urban municipality in Germany" (wd:Q42744322). For each municipality it returns:
- ?municipality — the Wikidata item IRI of the municipality
- ?municipalityLabel — the English label (when available)
- ?website — official website (P856), if available
- ?coord — coordinates (P625), if available (as a geo:wktLiteral)
- ?state — the administrative parent that is a German federated state (walking the P131 chain)
- ?stateLabel — English label of the federated state (when available)

The query returned 2,093 rows (i.e., 2,093 urban municipalities matched in Wikidata). If you want, I can:
- Export the full result table (CSV/JSON)
- Filter the list to a particular federated state
- Include German labels or additional properties (e.g., population)

Which output format would you prefer for the full list?

SPARQL query over wikidata:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?municipality ?municipalityLabel ?website ?coord ?state ?stateLabel WHERE {
  ?municipality wdt:P31 wd:Q42744322 .
  OPTIONAL {
    ?municipality rdfs:label ?municipalityLabel .
    FILTER ( LANG( ?municipalityLabel ) = "en" )
  }
  OPTIONAL {
    ?municipality wdt:P856 ?website
  }
  OPTIONAL {
    ?municipality wdt:P625 ?coord
  }
  OPTIONAL {
    ?municipality wdt:P131 + ?state .
    ?state wdt:P31 / wdt:P279 * wd:Q1221156 .
    OPTIONAL {
      ?state rdfs:label ?stateLabel .
      FILTER ( LANG( ?stateLabel ) = "en" )
    }
  }
}
ORDER BY ?municipalityLabel

Using entities:
- federated state of Germany (wd:Q1221156), also known as BL, Bundesland, German federal state, Land, state of Germany, etc.: is instance of designation for an administrative territorial entity of a single country / is subclass of federated state / is subclass of Federated state / is subclass of first-level administrative division / is subclass of administrative divisions of Germany / is subclass of NUTS 1 statistical territorial entity / administrative division of the Federal Republic of Germany
- urban municipality in Germany (wd:Q42744322), also known as city municipality of Germany, city of Germany: is subclass of place with town rights and privileges / is subclass of municipality in Germany / is subclass of city / municipality with town privileges in Germany (may e.g. legally prepend "Stadt" to its name)

Using properties:
- located in the administrative territorial entity (wd:P131 as wdt), also known as administrative territorial entity, administrative territory, city, happens in, in, etc.: is subproperty of location / is subproperty of part of / the item is located on the territory of the following administrative entity / the item is located on the territory of the following administrative entity / the item is located on the territory of the following ... the item falls only partially into the administrative entity. / the item is located on the territory of the following ... the item falls only partially into the administrative entity.
- subclass of (wd:P279 as wdt), also known as all members of this class also belong to, category of, form of, has superclass, is a class of, etc.: this item is a subclass (subset) of that item; ALL instances ... of volcanic landform); Everest is an instance of mountain / this item is a subclass (subset) of that item; all instances ... instance of volcanic landform); K2 is an instance of mountain / this item is a subclass (subset) of that item; all instances ... instance of volcanic landform); K2 is an instance of mountain / this item is a subclass (subset) of that item; all instances ... instance of volcanic landform); K2 is an instance of mountain
- instance of (wd:P31 as wdt), also known as example of, has type, is a, is an, is of type, etc.: that class of which this subject is a particular example and ... a subclass of mountain (and an instance of volcanic landform) / type to which this subject corresponds/belongs. Different ... and the volcano is a type (instance) of volcanic landform
- coordinate location (wd:P625 as wdt), also known as co-ordinate location, co-ordinates, coordinates, co-ords, coords, etc.: coordinates of the subject / coordinates of the subject / geocoordinates of the subject. For Earth, please note that only WGS84 coordinating system is supported at the moment
- official website (wd:P856 as wdt), also known as home page, homepage, home page (official), homepage (official), official homepage, etc.: is subproperty of URL / URL of the official page of an item (current or former). ... see the "Hijacked or dead websites" section of the Talk page

Execution result:
Got 2,093 rows and 6 columns, showing the first 5 and last 5 rows below:
| municipality             | municipalityLabel     | website                                  | coord                                       | state                             | stateLabel                       |
| ------------------------ | --------------------- | ---------------------------------------- | ------------------------------------------- | --------------------------------- | -------------------------------- |
| wd:Q3865                 |                       | <https://www.villingen-schwenningen.de/> | POINT(8.492962 48.063152) (geo:wktLiteral)  | Baden-Württemberg (wd:Q985)       | Baden-Württemberg (lang:en)      |
| Aach (wd:Q62158)         | Aach (lang:en)        | <http://www.aach.de/>                    | POINT(8.851667 47.845556) (geo:wktLiteral)  | Baden-Württemberg (wd:Q985)       | Baden-Württemberg (lang:en)      |
| Aachen (wd:Q1017)        | Aachen (lang:en)      | <https://www.aachen.de/>                 | POINT(6.083788 50.776207) (geo:wktLiteral)  | North Rhine-Westphalia (wd:Q1198) | North Rhine-Westphalia (lang:en) |
| Abenberg (wd:Q261822)    | Abenberg (lang:en)    | <https://www.abenberg.de/>               | POINT(10.961944 49.240555) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| Abensberg (wd:Q255698)   | Abensberg (lang:en)   | <https://www.abensberg.de/>              | POINT(11.850130 48.816720) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| ...                      | ...                   | ...                                      | ...                                         | ...                               | ...                              |
| Zwenkau (wd:Q10779)      | Zwenkau (lang:en)     | <https://www.zwenkau.de/>                | POINT(12.324167 51.217500) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |
| Zwickau (wd:Q3778)       | Zwickau (lang:en)     | <https://www.zwickau.de/>                | POINT(12.496111 50.718889) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |
| Zwiesel (wd:Q163091)     | Zwiesel (lang:en)     | <http://www.zwiesel.de>                  | POINT(13.233333 49.016667) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| Zwingenberg (wd:Q244821) | Zwingenberg (lang:en) | <https://zwingenberg.de/>                | POINT(8.613889 49.722500) (geo:wktLiteral)  | Hesse (wd:Q1199)                  | Hesse (lang:en)                  |
| Zwönitz (wd:Q58002)      | Zwönitz (lang:en)     | <https://www.zwoenitz.de/>               | POINT(12.813333 50.630278) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |

[System]
You are a question answering assistant. Your job is to generate a SPARQL query to answer a given user question.

You should follow a step-by-step approach to generate the SPARQL query:
1. Determine possible entities and properties implied by the user question.
2. Search for the entities and properties in the knowledge graphs. Where applicable, constrain the searches with already identified entities and properties.
3. Gradually build up the SPARQL query using the identified entities and properties. Start with simple queries and add more complexity as needed. Execute intermediate queries to get feedback and to verify your assumptions. You may need to refine or rethink your current plan based on the query results and go back to step 2 if needed, possibly multiple times.
4. Use the answer or cancel function to finalize your answer and stop the generation process.

Available knowledge graphs:
- wikidata at http://localhost:7001 with prefix index for entities and similarity index for properties, and notes:
  - P106 = occupation. Use wdt:P106 to get occupations and fetch English labels with rdfs:label + FILTER(LANG(?label)="en"). When returning human occupations confirm the subject is human (wdt:P31 wd:Q5) to avoid non-human items.
  - Population (P1082) and tabular (P4179): use wdt:P1082 for quick/direct values; for time-series/point-in-time use p:P1082 + ps:P1082 + pq:P585 and ORDER BY pq:P585 DESC. Inspect statement qualifiers (e.g., 'applies to part') and prov:wasDerivedFrom references (pr:P854 etc.) to detect city-proper vs metro/prefecture scopes before using values.
  - Common person properties: P31 (instance of human Q5), P106 (occupation), P569 (date of birth), P19 (place of birth), P27 (country of citizenship), P21 (sex/gender), P166 (award received). Use wdt: for direct values when possible.
  - P17 (country) can be incomplete/misleading for places (historical/overseas tags). To get modern cities in a country combine: instance-of city (P31=Q515) + wdt:P17 OR use wdt:P131* to find an admin parent whose wdt:P17 = the country. Also cross-check coordinates (P625) or admin hierarchy to spot outliers.
  - When filtering by date of birth (P569) use xsd:dateTime comparisons (e.g. >= "1900-01-01T00:00:00Z"^^xsd:dateTime). Be aware many items have year-only or imprecise dates and coverage is biased toward notable/public figures (sportspeople, politicians, artists).
  - To retrieve qualifiers (e.g., award year) use the statement/qualifier pattern: p:Pxxx / ps:Pxxx / pq:P585. Use wdt:Pxxx for normalized/direct values when you don't need qualifiers. Example: p:P166 + pq:P585 gives award year.
  - To get a person's occupations: use wdt:P106 for direct/normalized values and rdfs:label for human-readable names (FILTER(LANG(...))). Example: wd:Q... wdt:P106 ?occ . ?occ rdfs:label ?occLabel . FILTER(LANG(?occLabel)="en").
  - P166 = award received. Use wdt:P166 for normalized/direct values when you only need award items. To get award years or qualifiers use p:P166 / ps:P166 / pq:P585. Many statements lack pq:P585 — use OPTIONAL { ?stmt pq:P585 ?date } and ORDER BY DESC(?date). When listing award winners, filter ?person wdt:P31 wd:Q5 to ensure humans.
  - Population time-series pattern: wd:Q... p:P1082 ?stmt . ?stmt ps:P1082 ?pop . OPTIONAL { ?stmt pq:P585 ?date } . ORDER BY DESC(?date). pq:P585 is the point-in-time qualifier. IMPORTANT: time-series may mix scopes (city proper vs metro) — e.g., Tokyo has a 2016 entry ~38,140,000 likely metro vs smaller city figures. Inspect qualifiers/references for scope before use.
  - Use wdt:P131* to traverse the admin hierarchy and compare wdt:P17 on the place vs an admin parent to detect historical/colonial country assignments. When mismatches appear, inspect the admin parent's instance-of (P31), labels, qualifiers and references to interpret the historical scope.
  - wdt:P106 often yields multiple occupations for a person. Use GROUP_CONCAT(DISTINCT ?occLabel; separator=", ") or return multiple rows. Always fetch labels with rdfs:label + FILTER(LANG(?label)="en") to get readable occupation names.
  - When a search_entity returns multiple hits (person vs sculpture vs memorial), confirm you selected the human/statements by checking wdt:P31 (instance of) and relevant wdt: properties (e.g., wdt:P106). This avoids using a non-human QID by mistake.
  - search_autocomplete can be constrained with a SPARQL pattern containing ?search (e.g., SELECT * WHERE { ?search wdt:P31 wd:Q5 }). Use constraints to focus results (e.g., humans) and avoid ambiguous fallbacks; if it returns no results, fallback to search_entity and verify wdt:P31.
  - Use search_property_of_entity to find relevant properties for an entity (e.g., 'award' -> P166) and search_object_of_property to discover candidate objects (e.g., 'physicist' for P106). This reduces guessing P/Q ids and surfaces qualifiers.
  - Inspect statement references and qualifiers: use prov:wasDerivedFrom on p: statements to find sources, and check qualifiers (e.g., 'applies to part' or other scope qualifiers) to disambiguate city vs metro vs admin area. Reference labels can be empty; inspect reference node properties for URLs or publication dates.

Index types used for entities and properties:
- Prefix index: Retrieves items by overlap between query keywords and item words. The query keywords can match item words exactly or as prefixes.
- Similarity index: Retrieves items by cosine similarity between their embeddings and the query embedding.

General notes across knowledge graphs:
- Use rdfs:label + FILTER(LANG(?label)="en") for labels; avoid SERVICE wikibase:label. Use wdt:Pxxx for normalized/direct values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access statement qualifiers (e.g., award year via pq:P585). When dates matter, prefer xsd:dateTime and watch for year-only precision.
- Practical query patterns: use rdfs:label + FILTER(LANG(...)) for labels. Use wdt:Pxxx for direct/normalized values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access qualifiers (dates via pq:P585). For dates use xsd:dateTime and expect year-only precision. For places: combine P17 with P131 or coordinate (P625) filters to avoid historical/overseas mismatches.
- Use OPTIONAL for pq: qualifiers (e.g., pq:P585), sort by qualifier (ORDER BY pq:P585 DESC) to prefer recent values. Use GROUP_CONCAT to combine multiple labels/values in one row. Watch for typed literals (xsd:dateTime, xsd:decimal) when filtering or sorting.
- Before querying, use search_entity to resolve human-friendly names to wd:Q identifiers and verify instance-of (e.g., check wdt:P31 = wd:Q5 for humans) to avoid ambiguous matches. Prefer the top match but programmatically confirm P31 before using its data.
- Use search_property to confirm property identifiers (e.g., "country" -> P17) before embedding them in queries. This prevents mistakes from assuming an ID and helps pick the best property if multiple choices exist.
- search_autocomplete can fall back to an unconstrained search or error when there are too many candidates. If this happens (e.g., "more than the maximum supported number of subject candidates"), retry with narrower constraints such as instance-of (wdt:P31), additional type filters, or short prefixes to reduce the candidate set; otherwise fallback to search_entity and verify wdt:P31.
- Use list(...) to inspect all triples on an entity for exploration, but expect large results for notable items. Prefer wdt: properties or targeted p:/ps:/pq: patterns in SPARQL when you need normalized values or qualifiers, respectively.
- Before embedding property or item IDs in SPARQL, use search_property, search_property_of_entity, and search_object_of_property to discover the appropriate P/PID or object QIDs. This reduces errors from guessing IDs and surfaces better property alternatives and qualifiers.
- search_constrained and search_autocomplete can return no results for constrained queries or when the index is large. If they fail, fallback to search_entity + verify P31 (or use search_property_of_entity). Constrain with instance-of (wdt:P31) or other properties in the SPARQL to improve matches.
- When selecting awards by year, prefer xsd:dateTime range filters (e.g., >= "2001-01-01T00:00:00Z"^^xsd:dateTime && < "2002-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for portability and precision.
- When selecting award winners via wdt:P166, filter ?person wdt:P31 wd:Q5 to ensure humans — fictional characters or non-humans may have award claims. If you need award years use p:P166 / ps:P166 / pq:P585 and OPTIONAL { ?stmt pq:P585 ?year } to retrieve qualifier years.
- Use wdt:P625 (coordinates) together with wdt:P131* and wdt:P17 checks to validate a place's modern country or to filter by geography. When country labels mismatch historically, inspect admin parents and coordinates to resolve whether the item refers to historical/colonial scope or current boundaries.
- Prefer wdt:Pxxx for normalized/direct values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access qualifiers (e.g., pq:P585 for award year). Always fetch labels with rdfs:label + FILTER(LANG(?label) = "en").
- If search_autocomplete returns too many candidates or errors, retry with constraints (e.g., SELECT * WHERE { ?search wdt:P31 wd:Q5 }) or narrower query text. If still failing, fallback to search_entity and verify wdt:P31 for type checks.
- For award-year queries use p:P166 / ps:P166 / pq:P585 to access award statements and year qualifiers. Filter by xsd:dateTime ranges (e.g., >= "2020-01-01T00:00:00Z"^^xsd:dateTime && < "2021-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for robustness.
- Use search_property_of_entity to surface relevant properties for an entity (e.g., 'birth' -> P569/P19). list(...) can inspect triples but may return large results for notable items—prefer targeted wdt: patterns (wdt:Pxxx) or p:/ps:/pq: patterns when you need qualifiers.

SPARQL prefixes for use in function calls:
- bd: <http://www.bigdata.com/rdf#
- cc: <http://creativecommons.org/ns#
- data: <https://www.wikidata.org/wiki/Special:EntityData/
- dc: <http://purl.org/dc/elements/1.1/
- dct: <http://purl.org/dc/terms/
- foaf: <http://xmlns.com/foaf/0.1/
- geo: <http://www.opengis.net/ont/geosparql#
- geof: <http://www.opengis.net/def/function/geosparql/
- gn: <http://www.geonames.org/ontology#
- hint: <http://www.bigdata.com/queryHints#
- imdb: <https://www.imdb.com/
- math: <http://www.w3.org/2005/xpath-functions/math#
- ontolex: <http://www.w3.org/ns/lemon/ontolex#
- owl: <http://www.w3.org/2002/07/owl#
- p: <http://www.wikidata.org/prop/
- pq: <http://www.wikidata.org/prop/qualifier/
- pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/
- pqv: <http://www.wikidata.org/prop/qualifier/value/
- pr: <http://www.wikidata.org/prop/reference/
- prn: <http://www.wikidata.org/prop/reference/value-normalized/
- prov: <http://www.w3.org/ns/prov#
- prv: <http://www.wikidata.org/prop/reference/value/
- ps: <http://www.wikidata.org/prop/statement/
- psn: <http://www.wikidata.org/prop/statement/value-normalized/
- psv: <http://www.wikidata.org/prop/statement/value/
- qb: <http://purl.org/linked-data/cube#
- qfn: <http://qlever.cs.uni-freiburg.de/function#
- ql: <http://qlever.cs.uni-freiburg.de/builtin-functions/
- qlss: <https://qlever.cs.uni-freiburg.de/spatialSearch/
- rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
- rdfs: <http://www.w3.org/2000/01/rdf-schema#
- ref: <http://www.wikidata.org/reference/
- s: <http://www.wikidata.org/entity/statement/
- schema: <http://schema.org/
- skos: <http://www.w3.org/2004/02/skos/core#
- v: <http://www.wikidata.org/value/
- void: <http://rdfs.org/ns/void#
- wd: <http://www.wikidata.org/entity/
- wdno: <http://www.wikidata.org/prop/novalue/
- wdt: <http://www.wikidata.org/prop/direct/
- wdtn: <http://www.wikidata.org/prop/direct-normalized/
- wikibase: <http://wikiba.se/ontology#
- xsd: <http://www.w3.org/2001/XMLSchema#

Additional rules to follow:
- Explain your thought process before and after each step and function call.
- Do not just use or make up entity or property identifiers without verifying their existence in the knowledge graphs first.
- Do not ask the user for clarifications without first attempting to complete the task. If the task input is incomplete or ambiguous, try to make reasonable assumptions and document them.
- Do not use "SERVICE wikibase:label { bd:serviceParam wikibase:language ..." in SPARQL queries. It is not SPARQL standard and unsupported by the used QLever SPARQL endpoints. Use rdfs:label or similar properties to get labels instead.
- Always execute your final SPARQL query before giving an answer to make sure it returns the expected results.
- The SPARQL query should always return the actual identifiers / IRIs of the items in its result. It additionally may return labels or other human-readable information, but they are optional and should be put within optional clauses unless explicitly requested by the user.
- Do not stop early if there are still obvious improvements to be made to the SPARQL query. For example, keep refining your SPARQL query if its result contains irrelevant items or is missing items you expected.
- Do not perform additional computation (e.g. filtering, sorting, calculations) on the result of the SPARQL query to determine the answer. All computation should be done solely within SPARQL.
- For questions with a "True" or "False" answer the SPARQL query should be an ASK query.

[User] What are the urban municipalities in Germany? For each result, return also its official website, coordinates (if available), and parent federated state.

[Assistant]
Step 1 — interpret the question and identify candidate entities/properties:
- The user asks for "urban municipalities in Germany" and wants for each: official website, coordinates, and parent federated state.
- I'll search Wikidata for entities corresponding to "urban municipality", "urban municipality of Germany", or the German term "Stadt", "Gemeinde", "Stadtkreis", "kreisfreie Stadt" (district-free city), etc., and verify which concept matches the user's intent.
- I'll look up properties for official website (likely P856), coordinates (P625), and administrative parent/state (we'll find the appropriate property — likely P131 or P17 or a specific property linking municipalities to federated states; for German federated states, wdt:P17 = country, but to link municipality to state use wdt:P131 (located in the administrative territorial entity) or wdt:P300? We'll search.)

I'll start by searching for entities for "urban municipality", "municipality", "Stadt", "Gemeinde", and "kreisfreie Stadt".

Question: urban municipality

Top 10 entity alternatives:
1. urban municipality (wd:Q2616791), also known as urban gmina: administrative division of Poland / is subclass of gmina / is subclass of city
2. Municipio Urbaneja (wd:Q2023615, matched via Urbaneja Municipality), also known as Turístico Diego Bautista Urbaneja Municipality, Urbaneja Municipality: second-level administrative division in the Anzoátegui State, Venezuela / second-level administrative division in the Anzoátegui State, Venezuela / is instance of Municipalities of Venezuela / is instance of municipality of Venezuela
3. urban-rural municipality (wd:Q3491915), also known as urban-rural gmina: administrative division of Poland / is subclass of gmina
4. urban municipality in Germany (wd:Q42744322), also known as city municipality of Germany, city of Germany: municipality with town privileges in Germany (may e.g. legally prepend "Stadt" to its name) / is subclass of place with town rights and privileges / is subclass of municipality in Germany / is subclass of city
5. urban municipality in Sweden (wd:Q12015578): is subclass of administrative territorial entity of Sweden / is subclass of former administrative territorial entity / is subclass of Administrative divisions of Sweden / is subclass of administrative division of Sweden / is subclass of city / 1863–1970
6. Shanghai (wd:Q8686, matched via Shanghai Municipality), also known as Hu, Municipality of Shanghai, Pearl of the Orient, Shanghae, Shang-hai, etc.: is instance of Economic and Technological Development Zones / is instance of direct-administered municipality / provincial-level municipality in China / is instance of national central city / is instance of largest city / is instance of global city / is instance of Metropolis / is instance of metropolis / is instance of port city / is instance of big city / is instance of megacity
7. City of Brussels (wd:Q239, matched via Brussels (municipality)), also known as 02, Bru, Brussel, Brussels, Brussels, Belgium, etc.: municipality and capital city of Belgium / is instance of municipality of Belgium / is instance of federal capital / is instance of European City / is instance of big city
8. Florence (wd:Q2044, matched via Florence Municipality), also known as Firenze, Florence, Italy, Florence Municipality, Florence, Tuscany: Italian city and commune, located in Tuscany / is instance of Italian city-state / is instance of commune of Italy / is instance of big city / Italian comune / Italian comune
9. Honolulu (wd:Q18094, matched via Urban Honolulu), also known as Hanarourou, Honolulu CDP, Honolulu, Hawaii, Honolulu, HI, Urban Honolulu: city in and county seat of Honolulu County, Hawaii, United States, that is also the capital city of Hawaii / is instance of consolidated city-county / is instance of county seat / is instance of big city
10. Vientiane (wd:Q9326, matched via urban Vientiane), also known as Bientani, Nakhon Viangchan, urban Vientiane, Viang chan, Viangchan, etc.: is instance of conurbation / capital of Laos

[Assistant]
I'll search for the class "urban municipality in Germany" (found by autocomplete) to confirm its Q-id and then search for federated states (German states) to confirm how to link municipalities to states. I'll first list instances of the "urban municipality in Germany" class. I'll explain the plan and then run a query to retrieve items with instance-of = urban municipality in Germany, and for each include website (P856), coordinates (P625), and administrative parent (P131) and then filter that parent to be a German state (instance of "federal state of Germany"). I'll run iterative searches/queries to verify property IDs and results.

Question: urban municipality in Germany

Top 10 entity alternatives:
1. urban municipality in Germany (wd:Q42744322), also known as city municipality of Germany, city of Germany: municipality with town privileges in Germany (may e.g. legally prepend "Stadt" to its name) / is subclass of place with town rights and privileges / is subclass of municipality in Germany / is subclass of city
2. municipality in Germany (wd:Q262166), also known as Gemeinde, Municipalities of Germany: is subclass of fourth-level administrative country subdivision / lowest official level of territorial division in Germany / is subclass of fourth-level administrative division / is subclass of administrative divisions of Germany / is subclass of municipality / is subclass of LAU 2
3. urban municipality in Sweden (wd:Q12015578): is subclass of administrative territorial entity of Sweden / is subclass of former administrative territorial entity / is subclass of Administrative divisions of Sweden / is subclass of administrative division of Sweden / is subclass of city / 1863–1970
4. non-urban municipality in Germany (wd:Q116457956): municipality without town privileges in Germany / is subclass of municipality in Germany
5. Urban Infant Mortality in Imperial Germany (wd:Q39583217): scientific article published on December 1, 1994 / scientific article published on December 1994 / scientific article published on December 1994 / is instance of scholarly article / is instance of academic article
6. Urban planning in Nazi Germany (wd:Q30681249)
7. Urban Institute (Germany) (wd:Q77081223), also known as ui: company in Darmstadt, Germany / is instance of business
8. Urban-rural disparities in smoking behaviour in Germany (wd:Q33245883): scientific article published on 6 June 2006 / is instance of scholarly article / is instance of academic article
9. Urban Economic Development and Intergovernmental Policymaking in West Germany (wd:Q122039858): scientific article published in December 1986 / scientific article published in December 1986 / scientific article published in 1986 / is instance of scholarly article / is instance of academic article
10. Leptospirosis in urban wild boars, Berlin, Germany (wd:Q37334284): scientific article published on May 2007 / is instance of scholarly article / is instance of academic article

[Assistant]
I'll search for the entity representing "federal state of Germany" (German states — Bundesländer) to use it to filter the administrative parent of municipalities. Then I'll build a SPARQL query that selects items with instance-of = urban municipality in Germany and finds their P856 (official website), P625 (coordinates), and an administrative parent that is an instance of a German federal state. I'll explain each step and show the SPARQL I run.

Question: federal state of Germany

Top 10 entity alternatives:
1. Federal Statistical Office (wd:Q764739, matched via Federal Statistical Office of Germany), also known as Destatis, Federal Statistical Office of Germany, Statistisches Bundesamt: is instance of higher federal authority / is instance of registration authority / is instance of statistical service / is instance of federal office / German registration authority
2. Germany (wd:Q183, matched via Federal Republic of Germany), also known as BR Deutschland, Bundesrepublik Deutschland, Deutschland, Federal Republic of Germany: is instance of sovereign state / country in central Europe / country in central Europe / country in Central Europe / country in Central Europe / is instance of country
3. federated state of Germany (wd:Q1221156), also known as BL, Bundesland, German federal state, Land, state of Germany, etc.: is instance of designation for an administrative territorial entity of a single country / administrative division of the Federal Republic of Germany / is subclass of NUTS 1 statistical territorial entity / is subclass of administrative divisions of Germany / is subclass of first-level administrative division / is subclass of federated state / is subclass of Federated state
4. federal state of Austria (wd:Q261543), also known as province of Austria, state of Austria: is instance of designation for an administrative territorial entity of a single country / is subclass of administrative territorial entity of Austria / is subclass of NUTS 2 statistical territorial entity / is subclass of first-level administrative division / principal subdivision of Austria / is subclass of Federated state / is subclass of federated state
5. president of Germany (wd:Q25223, matched via Federal President of Germany), also known as Federal President of Germany, Federal President of the Federal Republic of Germany: is subclass of President of the Republic / is instance of public office / is subclass of head of state / head of state of Germany
6. Novorossiya (wd:Q16912926, matched via Federal State of Novorossia), also known as Federal State of Novorossia, Federal State of Novorussia, New Russia, Novorussia, Union of People's Republics: proposed merger of Donetsk and Lugansk People Republics that was never realized / is instance of proposed state merger / is subclass of fictional country / historical unrecognized state
7. Government of the Federal Republic of Germany (wd:Q313827, matched via Federal Cabinet of Germany), also known as breg, breg-de, Cabinet of Germany, Federal Cabinet of Germany, Federal German Cabinet, etc.: the government of the Federal Republic of Germany / is instance of federal government / is instance of executive branch / is instance of executive / is subclass of cabinet
8. Federal Chancellor of Germany (wd:Q4970706), also known as Chancellor, Chancellor of Germany, Chancellor of the Federal Republic of Germany, Federal Chancellor, Federal Chancellor of the Federal Republic of Germany, etc.: head of government of the Federal Republic of Germany (1949–present) / is subclass of Chancellor of Germany / is subclass of prime minister / is instance of public office
9. Federal State of Austria (wd:Q176495), also known as Ständestaat: sovereign state in Central Europe (1934–1938) / is instance of historical country
10. Federal Chancellery of Germany (wd:Q317027), also known as BKAmt, Bundeskanzleramt: Office of the Federal Chancellor of Germany / is instance of Oberste Bundesbehörde

[Assistant]
I'll run a SPARQL SELECT query that finds all items instance-of "urban municipality in Germany" (wd:Q42744322), and for each returns optional official website (P856), coordinates (P625), and an administrative parent up the P131 chain that is an instance (or subclass) of "federated state of Germany" (wd:Q1221156). I will also fetch English labels for the municipality and the state. Then I'll execute the query to verify results.

execute

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?municipality ?municipalityLabel ?website ?coord ?state ?stateLabel WHERE {
  ?municipality wdt:P31 wd:Q42744322 .
  OPTIONAL { ?municipality rdfs:label ?municipalityLabel . FILTER(LANG(?municipalityLabel) = "en") }
  OPTIONAL { ?municipality wdt:P856 ?website }
  OPTIONAL { ?municipality wdt:P625 ?coord }
  OPTIONAL {
    ?municipality wdt:P131+ ?state .
    ?state wdt:P31/wdt:P279* wd:Q1221156 .
    OPTIONAL { ?state rdfs:label ?stateLabel . FILTER(LANG(?stateLabel) = "en") }
  }
}
ORDER BY ?municipalityLabel

Got 2,093 rows and 6 columns, showing the first 5 and last 5 rows below:
| municipality             | municipalityLabel     | website                                  | coord                                       | state                             | stateLabel                       |
| ------------------------ | --------------------- | ---------------------------------------- | ------------------------------------------- | --------------------------------- | -------------------------------- |
| wd:Q3865                 |                       | <https://www.villingen-schwenningen.de/> | POINT(8.492962 48.063152) (geo:wktLiteral)  | Baden-Württemberg (wd:Q985)       | Baden-Württemberg (lang:en)      |
| Aach (wd:Q62158)         | Aach (lang:en)        | <http://www.aach.de/>                    | POINT(8.851667 47.845556) (geo:wktLiteral)  | Baden-Württemberg (wd:Q985)       | Baden-Württemberg (lang:en)      |
| Aachen (wd:Q1017)        | Aachen (lang:en)      | <https://www.aachen.de/>                 | POINT(6.083788 50.776207) (geo:wktLiteral)  | North Rhine-Westphalia (wd:Q1198) | North Rhine-Westphalia (lang:en) |
| Abenberg (wd:Q261822)    | Abenberg (lang:en)    | <https://www.abenberg.de/>               | POINT(10.961944 49.240555) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| Abensberg (wd:Q255698)   | Abensberg (lang:en)   | <https://www.abensberg.de/>              | POINT(11.850130 48.816720) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| ...                      | ...                   | ...                                      | ...                                         | ...                               | ...                              |
| Zwenkau (wd:Q10779)      | Zwenkau (lang:en)     | <https://www.zwenkau.de/>                | POINT(12.324167 51.217500) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |
| Zwickau (wd:Q3778)       | Zwickau (lang:en)     | <https://www.zwickau.de/>                | POINT(12.496111 50.718889) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |
| Zwiesel (wd:Q163091)     | Zwiesel (lang:en)     | <http://www.zwiesel.de>                  | POINT(13.233333 49.016667) (geo:wktLiteral) | Bavaria (wd:Q980)                 | Bavaria (lang:en)                |
| Zwingenberg (wd:Q244821) | Zwingenberg (lang:en) | <https://zwingenberg.de/>                | POINT(8.613889 49.722500) (geo:wktLiteral)  | Hesse (wd:Q1199)                  | Hesse (lang:en)                  |
| Zwönitz (wd:Q58002)      | Zwönitz (lang:en)     | <https://www.zwoenitz.de/>               | POINT(12.813333 50.630278) (geo:wktLiteral) | Saxony (wd:Q1202)                 | Saxony (lang:en)                 |

[Assistant]
I will finalize by returning the SPARQL query I executed and summarizing the results (number of rows and what each column contains).

answer

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?municipality ?municipalityLabel ?website ?coord ?state ?stateLabel WHERE {
  ?municipality wdt:P31 wd:Q42744322 .
  OPTIONAL { ?municipality rdfs:label ?municipalityLabel . FILTER(LANG(?municipalityLabel) = "en") }
  OPTIONAL { ?municipality wdt:P856 ?website }
  OPTIONAL { ?municipality wdt:P625 ?coord }
  OPTIONAL {
    ?municipality wdt:P131+ ?state .
    ?state wdt:P31/wdt:P279* wd:Q1221156 .
    OPTIONAL { ?state rdfs:label ?stateLabel . FILTER(LANG(?stateLabel) = "en") }
  }
}
ORDER BY ?municipalityLabel

Answer:
I executed the SPARQL query above on Wikidata. It returns items that are instances of "urban municipality in Germany" (wd:Q42744322). For each municipality it returns:
- ?municipality — the Wikidata item IRI of the municipality
- ?municipalityLabel — the English label (when available)
- ?website — official website (P856), if available
- ?coord — coordinates (P625), if available (as a geo:wktLiteral)
- ?state — the administrative parent that is a German federated state (walking the P131 chain)
- ?stateLabel — English label of the federated state (when available)

The query returned 2,093 rows (i.e., 2,093 urban municipalities matched in Wikidata). If you want, I can:
- Export the full result table (CSV/JSON)
- Filter the list to a particular federated state
- Include German labels or additional properties (e.g., population)

Which output format would you prefer for the full list?