7.4.2. Select by attributes

Let’s have a look at the result of the spatial query from the last section: In total eleven features were highlighted (that means selected) within the layer bees_colonies_locations.shp. So far so good, but what if the research question was to get the names of all communes containing honey bee colonies? To achieve this goal:

  1. Right-click the commune_boundary_FR.shp layer.
  2. Choose Open Attribute Table or use the button text icon21 within the Attributes toolbar.
    It is written in the headline of the Attribute table that the selected layer contains 51 entries in total (representing communes). Because they are topologically correct (see section 2.1.), none of these communities intersects with another and all the borders are congruent (that means that there are no blank “islands” in between).
  3. To focus on the selected features within a large dataset, use the checkbox named Show selected only within the Attributes table (Fig. 83). Browsing to the right within the different attributes, within the column called “GEMNAME”, the name of each community is given.
  4. The Attribute table can stay open while other functions are performed, e.g. during digitizing or editing tasks.

Assuming that the goal to produce a map that shows only the municipalities of the canton Fribourg, the following workflow can be applied:

  1. Right-click the commune_boundary_FR.shp layer.
  2. Select Query.
    The Query Builder dialog opens (Fig. 84). The attribute "KANTONSNR" contains an ID that represents the canton in Switzerland and in the case for Fribourg it is 10. So it is now our goal to restrict the features by means of a formula like "KANTONSNR" = 10. QGIS will then perform a restriction on the polygons displayed similar to an SQL Selection-Query by the use of a “where clause” (http://www.w3schools.com/sql/sql_where.asp).
  3. Try to rebuild the formula mentioned above by activating the options Fields, Operators and Values (otherwise it can also be written manually).
  4. Confirm the query with OK.
  5. Right-click on layer and Zoom to the layer extent.

Two polygons in the north-west outside of the canton of Fribourg will have disappeared from the Map View and will also no longer be selectable within the Attributes table (there are only 49 features left, Fig. 85.). However, they are not deleted from the file and can be restored by removing any SQL “where clause” in the Query Builder.

For other applications where a  section of a vector file should be selected without blanking out all the other features, the same Query Builder dialog can be accessed by the Attribute table > Advanced search. Of particular use is the combination of different “where clauses”, e.g. selecting attributes above or below a certain threshold or containing a given text string.

Fig. 83. Selected features (one single feature corresponds to one table entry) within the Attribute table. All in all, the layer consists of 51 features.


Fig. 84. The Query Builder is used to restrict the features to those within the canton of Fribourg (by the SQL where clause “KANTONSNR” = 10).


Fig. 85. Result after the SQL Query is performed: There are only 49 polygons left having the number 10 within the field “KANTONSNR”. Reproduced with permission of Swisstopo (BA13016).