MySQL query to perform selection using AND OR


Let us first create a table −

mysql> create table DemoTable
(
   StudentId int,
   StudentName varchar(20),
   StudentSubject varchar(20)
);
Query OK, 0 rows affected (0.62 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10,'Chris','MongoDB');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(11,'David','MySQL');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(12,'Bob','Java');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(10,'Carol','MySQL');
Query OK, 1 row affected (0.51 sec)
mysql> insert into DemoTable values(10,'Chris','MongoDB');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(14,'David','MySQL');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

Mysql> select *from DemoTable;

This will produce the following output −

+-----------+-------------+----------------+
| StudentId | StudentName | StudentSubject |
+-----------+-------------+----------------+
|        10 | Chris       | MongoDB        |
|        11 | David       | MySQL          |
|        12 | Bob         | Java           |
|        10 | Carol       | MySQL          |
|        10 | Chris       | MongoDB        |
|        14 | David       | MySQL          |
+-----------+-------------+----------------+
6 rows in set (0.00 sec)

Following is the query to perform selection using AND OR −

mysql> select *from DemoTable where StudentName='David' and StudentSubject='MySQL' OR StudentId=11;

This will produce the following output −

+-----------+-------------+----------------+
| StudentId | StudentName | StudentSubject |
+-----------+-------------+----------------+
|        11 | David       | MySQL          |
|        14 | David       | MySQL          |
+-----------+-------------+----------------+
2 rows in set (0.00 sec)

Updated on: 09-Oct-2019

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements