MySQL Tutorial
MySQL WHERE clause is used to specify a condition while fetching the data from a single table.
MySQL WHERE clause is used to extract only those records that fulfill a specified condition
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Here, column1, column2, column3, ... are the field names of the table you want to select data with where condition.
If you want to select all the fields available in the table with where condition, use the following syntax:
SELECT *
FROM table_name
WHERE condition;
Let's put these statements into real use.
We've a 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 | SQL |
6 | Lara |
SELECT ROLL_NO, NAME FROM Students
WHERE ROLL_NO > 2;
The above SELECT statement selects two columns(ROLL_NO and NAME) from the above Students table.
ROLL_NO | NAME |
---|---|
3 | Sara |
4 | Rim |
5 | Micheal |
6 | Lara |
Now we will select all the fields if ROLL_NO is greater than 3 available in the table.
SELECT * FROM Students
WHERE ROLL_NO > 3;
The above SELECT statement selects all columns from the Students table.
ROLL_NO | NAME | SUBJECT |
---|---|---|
4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara |
Now we will select all the fields if SUBJECT is equal to java available in the table.
SELECT * FROM Students
WHERE SUBJECT = 'Java';
The above SELECT statement selects all columns if subject is equal to Java from the Students table.
ROLL_NO | NAME | SUBJECT |
---|---|---|
4 | Rim | Java |
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 SQL 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 |