Getting list of distinct values from shapefile field using QGIS?

Update answer (QGIS Version >= 2.14)

Since QGIS 2.14, you can use run SQL statements on any loaded vector layer using Virtual layers.

  1. Having the layer loaded in QGIS, go to Layer > Add Layer > Add/Edit Virtual Layer;
  2. In the Create virtual layer dialog, enter you SQL statement in the Query field. Something like:

    SELECT DISTINCT city_name FROM layer_name

  3. For geometry set No Geometry

  4. Click Ok and a table will load in QGIS with the desired unique values.

Note: this table will be updated if new values are added to the city_name column.

Legacy answer (QGIS Version < 2.14)

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.


Use the QGIS DB Manager and access your shapefile via 'virtual layers'

You can then use the SQL window and write your query:

enter image description here


QGIS now has a tool that will list unique values in one or more fields in a layer. "This algorithm generates a report with information about the unique values found in a given attribute (or attributes) of a vector layer."

Can be found in main menu Vector> Analysis Tools> List Unique Values. Generates a table with unique values.