MySQL SQL: how to select rows with even or odd values

The simplest way to fetch the rows with even or odd numbers is to divide the column value by 2 and check the reminder. If the reminder is 0, the number is even and, if not, it's odd.

MySQL, PostgreSQL and Oracle

MySQL, PostgreSQL and Oracle databases have the function MOD which we can use to check the reminder. Here is an SQL example that fetches all the rows with an even number:
SELECT * FROM my_table WHERE MOD(my_column, 2) = 0;
If we want to fetch all the rows which have an odd number, we can do it like that:
SELECT * FROM my_table WHERE MOD(my_column, 2) <> 0;
In both examples you need to replace my_table with the name of your table and my_column with the name of the column you want to check if it's even or odd.

MSSQL Server

MSSQL Server does not have a MOD function and instead, we have to use the % (modulo) operator. In order to fetch all the rows with an even column value, run:
SELECT * FROM my_table WHERE my_column % 2 = 0;
Otherwise, if you want to fetch all the rows with an odd column value, run:
SELECT * FROM my_table WHERE my_column % 2 <> 0;

Looking for a backup solution? Try Weap.io

Simple & flexible backup solution to keep your servers, websites & databases safe.

Start free trial
App screenshot