WHERE clause with Multiple Conditions

The MySQL And, Or, Not, operators can be combined in a SELECT, INSERT, UPDATE, or DELETE statement.

When combining these operators, it is important to use parentheses so that the database knows knows what order to evaluate each condition.

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
WHERE condition 1 AND condition2 ... OR condition_n;
WHERE condition 1 AND condition2 ... OR condition_n;
WHERE condition 1 AND condition2 ... OR condition_n;

Let’s look at an example that combines the AND, OR conditions in a SELECT statement. To do this, Click show query box ( to edit the query)

Then execute the query below;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Select * from products where price >2000 and quantity=5
Select * from products where price >2000 and quantity=5
Select * from  products where price >2000 and quantity=5

Look at the following example will to display all the Male students who were joined in DCA course.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * FROM 'students' WHERE course='DCA' and gender = 'MALE'
SELECT * FROM 'students' WHERE course='DCA' and gender = 'MALE'
SELECT * FROM 'students' WHERE course='DCA' and gender = 'MALE'

If we replace and with or operator in the above example, it will display all the Male students of all courses as well as, all the students joined in DCA course.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Select * From `students` where course ='DCA' or gender= 'Male'
Select * From `students` where course ='DCA' or gender= 'Male'
Select * From `students` where course ='DCA' or gender= 'Male'

Suppose, we want to display students name of all students who were joined in DCA and HDCA course, the query will be,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
select * from `students` where course='DCA' or course='HDCA'
select * from `students` where course='DCA' or course='HDCA'
select * from `students` where course='DCA' or course='HDCA'

Let’s look at the following example with AND and OR operator ,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * FROM `STUDENTS` WHERE (course = 'DCA' or course = 'HDCA') and gender = 'FEMALE'
SELECT * FROM `STUDENTS` WHERE (course = 'DCA' or course = 'HDCA') and gender = 'FEMALE'
SELECT * FROM `STUDENTS` WHERE (course = 'DCA' or course = 'HDCA') and gender = 'FEMALE'

Displays all the students who were joined either in DCA or HDCA course.

The following example explains using NOT operator.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
select * FROM `students` WHERE not course = 'DCA'
select * FROM `students` WHERE not course = 'DCA'
select * FROM `students` WHERE not course = 'DCA'

Displays all the students expect those joined in DCA course.