close[x]


MySQL

MySQL-Home MySQL-Environment setup MySQL- Workbench MySQL-Basic syntax MySQL-Operator MySQL-Data type MySQL-Comments MySQL-Create DB MySQL-Drop DB MySQL-Select DB MySQL-Create Table MySQL-Drop table MySQL-Truncate MySQL-Primary Key MySQL-Foreign Key MySQL-Null MySQL-Increment MySQL-Having MySQL-Top MySQL-Insert Statement MySQL-Select Statement MySQL-Alter Statement MySQL-Where MySQL-And & Or MySQL-Default values MySQL-Exists MySQL-Order by MySQL-View MySQL-Update Statement MySQL-Delete Statement MySQL-Like MySQL-Sort MySQL-Limit MySQL-Min MySQL-Max MySQL-Group MySQL-In MySQL-Between MySQL-Union MySQL-Count MySQL-Average MySQL-Sum MySQL-Date & Time MySQL-Import MySQL-Export MySQL-Index MySQL-Temporary MySQL-Join MySQL-Full Join MySQL-Inner Join MySQL-Left Join MySQL-Right Join MySQL-Store Procedure MySQL-Injection MySQL-PHP connection



learncodehere.com



MySQL - AND and OR Operators

MySQL WHERE clause is used to extract only those records that fulfill a specified condition

MySQL WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition.

This table named Students in our database that contains the following records:


ROLL_NO NAME SUBJECT
1 Will C++
2 SAM Python
3 Sara HTML
4 Rim Java
5 Micheal MySQL
6 Lara

The AND Operator

The AND operator displays a record if all the conditions separated by AND are TRUE or returns TRUE only if both condition evaluate to TRUE

Syntax : AND Operator


SELECT column1, column2, columnN....
FROM table_name
WHERE condition1 AND condition2;
                

Let's check out some examples that demonstrate how it actually works.

We can Select all columns(*) from table with the AND operator.

Example :


SELECT * FROM Students
WHERE ROLL_NO < 6 AND SUBJECT = 'Python';

Result

ROLL_NO NAME SUBJECT
2 SAM Python


The OR Operator

The OR operator displays a record if one of the conditions separated by or are TRUE or returns TRUE only if one condition evaluate to TRUE

Syntax : OR Operator


SELECT column1, column2, columnN....
FROM table_name
WHERE condition1 OR condition2;
            

Let's check out some examples that demonstrate how it actually works.

We can Select all columns(*) from table with the AND operator.

Example :


SELECT * FROM Students
WHERE ROLL_NO > 4 OR SUBJECT = 'Python';

Result

ROLL_NO NAME SUBJECT
2 SAM Python
5 Micheal MySQL
6 Lara

Operators Allowed in WHERE Clause

The following operators can be used in the WHERE clause:

Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of MySQL this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column