MySQL Tutorial
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 displays a record if all the conditions separated by AND are TRUE or returns TRUE only if both condition evaluate to TRUE
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.
SELECT * FROM Students
WHERE ROLL_NO < 6 AND SUBJECT = 'Python';
ROLL_NO | NAME | SUBJECT |
---|---|---|
2 | SAM | Python |
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
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.
SELECT * FROM Students
WHERE ROLL_NO > 4 OR SUBJECT = 'Python';
ROLL_NO | NAME | SUBJECT |
---|---|---|
2 | SAM | Python |
5 | Micheal | MySQL |
6 | Lara |
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 |