Menu Close

MySQL OR Operator

MySQL OR Operator

In this MySQL tutorial, you will learn everything about MySQL OR operator with the help of examples. OR operator in MySQL is used to combine more than one condition but only one condition will be True.
In the previous MySQL tutorial, we have seen all about, MySQL SELECT DISTINCT statement to eliminate duplicate rows from the result set.

MySQL OR Operator Examples

In MySQL, AND Operator is used to combining more than one expression or condition together. The only result will come, satisfied any one condition separated by OR operator.

Syntax

The following is the syntax of the OR operator.

SELECT select_list FROM table_name WHERE condition1 OR condition2....;

MySQL OR Operator Examples

Here we are going to use OR operator with the help of examples to specify more than one condition. We will use students table, Which contains the following records.

Example

SELECT * FROM students;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     1 | Vishvajit  | Rao       | MCA    | 2021-11-13 14:26:39 |
|     2 | John       | Doe       | Mtech  | 2021-11-13 14:26:39 |
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-13 14:26:39 |
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
|     6 | Aysha      | Garima    | BCA    | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+

a). Let’s select all the records from the students table where last_name is Singh or course is MCA.

Example

SELECT * FROM students where last_name ='Singh' OR course = 'MCA';

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     1 | Vishvajit  | Rao       | MCA    | 2021-11-13 14:26:39 |
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+

Note:- You can verify the result from the above table.

b). Here we are going to select students where last_name is Kumar or st_is is greater than 5.

Example

SELECT * FROM students where last_name ='Kumar' OR st_id > 5;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-13 14:26:39 |
|     6 | Aysha      | Garima    | BCA    | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+

Summary

Here we have seen all about MySQL OR operator to apply more than one expression together with the help of the examples. You have to remember one thing about OR Operator.OR Operator displays the records if any of the conditions separated by OR is True.

OR Operator in MySQL perform the following:-

  • MySQL OR operator combine one or more condition together.
  • MySQL Returns True if any of the condition satisfied other its returns False.
  • OR Operator used with MySQL WHERE Clause.

MySQL Tutorial:- Click Here

Ref:- Click Here

Thanks for reading …..

MySQL WHERE Clause
MySQL IN Operator

Related Posts