Queries in QGIS pt 1 – Attribute Queries
Quick Select Tools
The Select Flyout button on the Main toolbar contains tools used to quickly select features:
Attribute Queries
One method to select features in a layer is to select features using an attribute query:-
To query a layer by its attribute data,
- Right click on the layer’s name in the Layer Panel
- Select Query from the short cut menu to open the Query Builder box
-
It is possible to type the SQL query into the SQL where clause box. Alternatively:-
- Click the Field that contains the attributes you wish to base the query on
- Select the operator
- Press the All under the values box to list all the available values
- Double click the value that you wish to select
- You will see the query being built in the SQL where clauses box.
- Click the Field that contains the attributes you wish to base the query on
Boolean operations (And, Or, Not)
- And: narrows the search by requiring all the criteria to be present. It is most commonly used when the criteria are in different columns. E.g. Select Business_type = “bank” And City = “London” will select banks with London addresses.
- Or: broadens the search criteria by requiring just one of several criteria to be true. It is most commonly used to give different options to the same column e.g. select Business_type = “bank” Or Business_type = “shop” will select all the banks and shops within the data set.
- Not: returns records that do NOT match the criteria e.g. select Business_type = “bank” AND City NOT “London” would return all the banks that do not have a London address. Note that QGIS uses != for not equal to
It is important to be clear about when to use And Or operators.
If the first example was worded Select Business_type = “bank” OR City = “London” this would return all the banks and any property with a London address
If the second example was worded select Business_type = “bank” AND Business_type = “shop” this would return only those business which had bank and shop entered as business type. This would probably be none at all as businesses tend to either be a shop or a bank but rarely both!
Mathematical Operations
It is possible to use mathematical formulae to select from columns that are formatted in a numeric format (e.g. integer):-
>Greater than e.g. Business_type = “shop” AND floor_space > 100
< Less than e.g. Business_type = “shop” AND floor_space < 100
>= greater than or equal to
<= Less than or equal to
!= Does not equal