Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a polygon layer. I would like to select every second row of the polygons but not manually. Is there an expression to select every second row as seen on the picture with Select by expression in QGIS?

*(Numbers refer to the polygon's "ID")

Numbers refer to the polygon's ID

share|improve this question
up vote 8 down vote accepted

You can use the floor function in your expression, like this:

floor($id /24) IN (1,3,5,7,9,11,13,15,17,19,21)

On a sample grid I've obtained this result with my own numbers (my grid has 13 columns per row):

enter image description here

I've used: floor($id/13) IN (1,3,5,7,9)

So, in general:

floor($id / number_of_columns) IN (1, 3, 5, ..., 2*number_of_rows_to_be_selected - 1)

To select exactly the other rows (i.e., 1st, 3rd, and so on) you would use:

floor($id /24) IN (0,2,4,6,8,10,12,14,16,18,20,22)


floor($id / number_of_columns) IN (0, 2, 4, ..., 2*number_of_rows_to_be_selected)

Does it make sense?

EDIT: Improved answer

Thanks to @ndawson, who suggests a shorter version of the selection expression. In summary, you could use:

floor($id / number_of_columns) % 2 = 1

to select 2nd, 4th, 6th, ... rows, or:

floor($id / number_of_columns) % 2 = 0

to select 1st, 3rd, 5th, ... rows.

share|improve this answer
Perfect, exactly this is what I was looking for! Thank you so much! – Skye Feb 12 at 19:38
You're welcome! – Germán Carrillo Feb 12 at 19:42
The floor function is brilliant! Thanks! I learned something very useful today! – jbgramm Feb 12 at 20:11
One more thing came to mind: In case I had awful lot of rows, could I simplify somehow the expression's "number of rows to be selected"-part after the "IN" operator? For example, if I had 1000 rows but I only needed every even-numbered row: (2,4,6,8,....,996,998,1000) could I create some kind of a range-ish expression instead of typing each number one-by-one? (I've tried a few things but none of them worked) – Skye Feb 13 at 19:59
Try "floor($id /24) % 2 = 1" – ndawson Feb 13 at 20:19

You can use the "select by expression" geoalgorithm with an expression like id>= and id<=24 or id>=49 and id<=72..... Since the cell values seem to be numbered by standard count laterally, it shouldn't be a problem to figure out the ranges you need even if your data set contains more rows than listed above.

share|improve this answer
Yes, this works, but it takes at least as much time to type all the intervals as if I selected each row one by one manually. It would be great to find a faster solution if possible. – Skye Feb 12 at 20:05

Make sure the layer you wish to choose is highlighted in the Layer Control Box Then using the Select By Expression button

Type what you see in the picture into the Expression Box.

It should look like this when you are finished

(( "ID" ) - 1) % 24 = 0

If the expression is correct you will see a result in the bottom of the Expression Builder Dialog that does not "Expression is Invalid" in red letters.

ID is the name of the field you wish to select, the first polygon of each row minus 1 is divisible by 24.

% is the modulo function, it will take the number immediately following, and divide the ID -1 number by it. If the remainder is not 0, the polygon is not chosen.


Ignore the error displayed in the above picture, I just did this with a table that was not a grid, and did not have a field named ID.

share|improve this answer
I think you found a way to select by column, which could also be useful for other use cases. – Germán Carrillo Feb 12 at 20:02
You are absolutely right! I totally misread, and misunderstood the question. – jbgramm Feb 12 at 20:08
Now I know how to select the first column (which is a new information for me). I wonder whether it would be achievable to select every second column somehow with the help of your expression – Skye Feb 12 at 20:18
I am trying to wrap my head around the SQL logic to be able to choose the odd, or even numbered rows using the % function rather than explicitly typing them It would be something like this, but I am unable to see if it works at this time. floor($id /24) IN ( (% 2 = 0)) for even numbered rows floor($id /24) IN ( (% 2 = 1)) for odd numbered rows Or something like that. – jbgramm Feb 12 at 20:29
I think you would need a list and the IN operator to select more than one column, something like $id % 24 IN (1,3,5,7,9,11,13,15,17,19,21,23) – Germán Carrillo Feb 12 at 20:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.