Select the even / odd number records from a table

Chloe Wong
Feb 10, 2021

I worked on SQL training few days ago and learnt how to execute the query with even/odd number criteria.

SCENARIO:

Query a list of BUS STOP NAME from the BUS STOP table that has an even ID number. Print the results in any order, but exclude duplicates from the answer.
The BUSSTOP table is described as follows:

top 1000 rows for bus stop table in SSMS

To select all the distinct even number records from a table:

SELECT DISTINCT stop_id,busstopnameFROM dbo.busstopWHERE stop_id %2 = 0

To select all the distinct odd number records from a table:

SELECT DISTINCT stop_id,busstopnameFROM dbo.busstopWHERE stop_id %2! = 0

--

--