MySQL Tutorial
The WHERE
clause in MySQL is used to filter records from a table based on a specified condition. It is commonly used with SELECT
, UPDATE
, DELETE
, and INSERT
statements.
Below is a selection from the "Students" table in the school_db database:
student_id |
name |
gender |
city |
age |
score |
course_id |
|
phone_number |
---|---|---|---|---|---|---|---|---|
1 |
Alice |
Female |
Delhi |
20 |
85 |
101 |
NULL |
1234567890 |
2 |
Bob |
Male |
Mumbai |
22 |
75 |
NULL |
bob@email.com |
NULL |
3 |
Charlie |
Male |
Delhi |
21 |
95 |
102 |
NULL |
NULL |
4 |
David |
Male |
Bangalore |
23 |
65 |
101 |
david@email.com |
NULL |
5 |
Eve |
Female |
Mumbai |
20 |
80 |
103 |
NULL |
NULL |
6 |
Frank |
Male |
Delhi |
22 |
90 |
103 |
NULL |
NULL |
7 |
Alice |
Female |
Mumbai |
19 |
60 |
102 |
NULL |
NULL |
SELECT column1, column2 FROM table_name WHERE condition;
UPDATE table_name SET column1 = value1 WHERE condition;
student_id
is 5.DELETE FROM table_name WHERE condition;
The following operators can be used in the WHERE
clause:
Operator |
Description |
Example |
---|---|---|
= |
Equal to |
Try it yourself |
|
Not equal to |
Try it yourself |
> |
Greater than |
Try it yourself |
< |
Less than |
Try it yourself |
>= |
Greater than or equal to |
Try it yourself |
<= |
Less than or equal to |
Try it yourself |
BETWEEN |
Between a certain range |
Try it yourself |
LIKE |
Search for a pattern |
Try it yourself |
IN |
To specify multiple possible values for a column |
Try it yourself |