MySQL query to find all rows where ID is divisible by 4?


Let us first create a table with one of the column as ID −

mysql> create table DemoTable
   (
   ID int,
   StudentName varchar(10),
   CountryName varchar(20)
   );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(0,'David','AUS');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(3,'Chris','UK');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values(8,'Carol','US');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values(9,'Sam','US');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(12,'Robert','UK');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values(10,'Mike','AUS');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+------+-------------+-------------+
| ID   | StudentName | CountryName |
+------+-------------+-------------+
| 0    | David       | AUS         |
| 3    | Chris       | UK          |
| 8    | Carol       | US          |
| 9    | Sam         | US          |
| 12   | Robert      | UK          |
| 10   | Mike        | AUS         |
+------+-------------+-------------+
6 rows in set (0.00 sec)

Following is the query to find all rows where ID is divisible by 4 −

mysql> select *from DemoTable where (ID%4)=0;

Output

+------+-------------+-------------+
| ID   | StudentName | CountryName |
+------+-------------+-------------+
| 0    | David       | AUS         |
| 8    | Carol       | US          |
| 12   | Robert      | UK          |
+------+-------------+-------------+
3 rows in set (0.30 sec)

Updated on: 30-Jul-2019

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements