QGIS SQL Syntax – Selecting Features by Expression in QGIS

attribute-tablemaximumqgisselect-by-attributesql

I have a table (shapefile) in QGIS where I want to select features that satisfies this condition:

group by a field ("join_label") and within the "grouped field", pick the attribute with the highest area (the area has already been calculated in a separate field)

I tried to do this:

maximum("Area", group_by:="join_label")

This expression selects everything in the table.

I wanted to use the "Execute SQL" function but how do I write the query?

Best Answer

Using the Execute SQL function:

SELECT
 join_label,
 MAX("Area") as max_area,
 geometry
FROM
  input1
GROUP BY
  join_label

Not forgetting to select the layer in the Additional input datasources at the top of the tool window (which gets the name input1 in your expression).

Related Question