How can we fetch alternate even-numbered records from MySQL table?



To understand this concept we are using the data from table ‘Information’ as follows −

mysql> Select * from Information; +----+---------+ | id | Name    | +----+---------+ |  1 | Gaurav  | |  2 | Ram     | |  3 | Rahul   | |  4 | Aarav   | |  5 | Aryan   | |  6 | Krishan | +----+---------+ 6 rows in set (0.00 sec)

Now, the query below will fetch the alternate even-numbered records from the above table ‘Information’ −

mysql> Select id,Name from information group by id having mod(id,2) = 0; +----+---------+ | id | Name    | +----+---------+ |  2 | Ram     | |  4 | Aarav   | |  6 | Krishan | +----+---------+ 3 rows in set (0.00 sec)
Updated on: 2020-06-22T12:26:48+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements