Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shapefile whose features I can filter by specifying a where clause in the query dialogue. F.x. I have a field named 'city_name' and by stating 'city_name = "London"' in the where clause only London is displayed. What I would like to do now is to fetch all values in 'city_name' from the attribute table.

Something like that:

select distinct city_name from [attribute table]

I found several tools in QGIS apparently dealing with SQL and I also had a quick look at the different Plugins. But I fail at connecting to a database or the table name ... what database or which table in that case?

In the end I want to export the result list and use it for further processing.

I am using QGIS 1.8.

share|improve this question
up vote 16 down vote accepted

You have a few choices to do what you ask.

  1. Import your shapefile in a Spatialite or Postgis database, and then you can query your table using complete SQL statements;
  2. Use the Dissolve tool (Vector > Geoprocessing Tools > Dissolve), to dissolve your shapefiles using the field "city_name". Although is an strange method, the dbf file of the resulting shapefile will provide the list you need;
  3. Take a look at group stats plugin (1.6), you can use "city_name" as classification field, and press calculate. It will calculate some stats about each city, you can then copy the result and extract the city list.

I have just noticed that, in the Vector > Analysis Tools, there is a List unique Values tool that is precisely what one needs for this task. So easy... no workarounds and no need for Plugins.

share|improve this answer
#3 did the trick! – Raffael Jan 27 '13 at 19:30

Try including something like:

where city_name is NOT NULL or not equal to NULL
share|improve this answer
this will lead to QGIS displaying all features with a city_name. but I want to export the city_name values. – Raffael Jan 27 '13 at 19:15

Use query builder in qgis, go to city name field in fields in query builder. Go to values, add all. delete any nulls. build a script similar to city name = "london" and city name = "paris" untill you have the complete list of names and run the script. Now all city names are selected. you can export selected as a shape file and import into a postgresql database. To use a postgresql backend to qgis install a postgresql stack with geoserver, postgresql, and a postgis enabled databse in the postgresql. Store your data as post gis in the postgis database. You will need to connect to server. play around a bit and you will figure it out.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.