Outdated version of the documentation. Find the latest one here.

Query Builder

The Query Builder allows you to define a subset of a table using a SQL-like WHERE clause and display the result in the main window. The query result then can be saved as a new vector layer.

Query

Open the attribute table choosing mActionOpenTable Open Attribute Table from the menu bar. Click on the [Advanced Search] button to open the Query builder. For example, if you have a towns layer with a population field you could select only larger towns by entering population > 100000 in the SQL box of the Query builder. Figure_attributes_2 shows an example of the Query builder populated with data from a PostGIS layer with attributes stored in PostgreSQL. The Fields, Values and Operators sections help the user to construct the SQL-like query.

Figure Attributes 2:

../../../_images/queryBuilder.png

Query Builder nix

The Fields list contains all attribute columns of the attribute table to be searched. To add an attribute column to the SQL where clause field, double click its name in the Fields list. Generally you can use the various fields, values and operators to construct the query or you can just type it into the SQL box.

The Values list lists the values of an attribute table. To list all possible values of an attribute, select the attribute in the Fields list and click the [all] button. To list the first 25 unique values of an attribute column, select the attribute column in the Fields list and click the [Sample] button. To add a value to the SQL where clause field, double click its name in the Values list.

The Operators section contains all usable operators. To add an operator to the SQL where clause field, click the appropriate button. Relational operators ( = , > , ...), string comparison operator (LIKE), logical operators (AND, OR, ...) are available.

The [Test] button shows a message box with the number of features satisfying the current query, which is usable in the process of query construction. The [Clear] button clears the text in the SQL where clause text field. The [Save] and [Load] button allow to save and load SQL queries. The [OK] button closes the window and selects the features satisfying the query. The [Cancel] button closes the window without changing the current selection.

Tip

Changing the Layer Definition

You can change the layer definition after it is loaded by altering the SQL query used to define the layer. To do this, open the vector Layer Properties dialog by double-clicking on the layer in the legend and click on the [Query Builder] button on the General tab. See Section The Vector Properties Dialog for more information.

Selection

With QGIS it is possible also to select features for visualisation using a similar query builder interface to that used in Query Builder. In the above section the purpose of the query builder is to highlight all selected features meeting the filter criteria in yellow. The rest of the features do not change their appearance.

Another possibility is to make a selection by right clicking on a layer in the legend and choosing Query .... If you perform a SQL query here only the selected features are displayed in the map view. Features that don’t match the SQL query are not visualised in the map view. The same function is available from the General tab in the vector Layer Properties dialog. You can find a [Query builder] button right below. The selection described above can be used with all vector data providers.

Save selected features as new layer

The selected features can be saved as any OGR supported vector format and also transformed into another Coordinate Reference System (CRS). Just open the right mouse menu of the layer and click on Save selection as ‣ to define the name of the output file, its format and CRS (see Section Map Legend). It is also possible to specify OGR creation options within the dialog.