[Qgis-user] Select features based on an array with elements from another layer

Ann-Christin Voss ann-christin.voss at geoas.de
Wed Jul 8 03:55:55 PDT 2026


Dear list,

I try to select some features vie their id(integer) based on an array containing ids (also integers) I get from another layer.

In SQL I would write SELECT * FROM base WHERE id in (SELECT DISTINCT id FROM filter_layer WHERE ...filter...)

Naturally I would write the expression in 'Select features by expression' like this but it dosen't work (no features selected):
array_contains(
    array_distinct(
        aggregate(
            'filter_layer',
            'array_agg',
            "ID",
            ...filter...
        )
    )
"id")

I would assume it iterates through the ids in the base and if it's found in the array it's true and therefore selected and if it's not in there, it's false and therefore not selected.

Instead I have to  write this (but it also would't work if didn't have unique id's in the base layer because it then would only select the first found):
array_find(
    array_distinct(
        aggregate(
            'filter_layer',
            'array_agg',
            "id",
            ...filter...
        )
    ),
    "id"
) <> -1

(https://gis.stackexchange.com/questions/344412/select-features-matching-the-values-in-an-array-qgis-3-10-expression)


I simplified some things for testing and try to select in the base_layer without elements from another layer:
"id" IN (10,11,12,13) works (10 and 11 true --> selected, 12 and 13 false --> not selected)
"id" IN array(10,11,12,13) doesn't work (no Objects selected)

I find this very hard to understand and I don't know how to explain this to costumers who before used programs where they select based on sql expressions.
Also I can't figure out how to select the features if the id(in this case) isn't unique (see question above).
I chose the "select by expression" tool because I need the selection in the base_layer and not a separate layer containing the filtered features.
Could someone please explain it to me or tell me a more "natural feeling" way to select the features based on an array?
(I'm using QGIS 3.44)

Many thanks in advance and best regards,
Ann-Christin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20260708/1a466f88/attachment.htm>


More information about the QGIS-User mailing list