What is MySQL WHERE Condition?

The MySQL WHERE condition filters which records need to be selected from the table. By using the conditions we can filter the table records that need to be selected.

Syntax:

SELECT column_name1, column_name2, ..., column_name_n FROM schema_name.table_name WHERE condition;

We have created and students table in the MySQL SELECT Statement, we will use the same table for selecting the records based on the where conditions,

  1. Condition for varchar value.

If we want to add a condition for the varchar column we need to include that value in single quotes.

Example 1:

select * from test_schema1.students where name = 'KMS';

Example 2:

select * from test_schema1.students where gender = 'M';
  1. Condition for numeric value.

For numeric values no need to add single quotes we can directly give the value as shown below.

Example 1:

select * from test_schema1.students where marks > 400;

Example 2:

select * from test_schema1.students where marks = 250;

Even if we add this numeric value in single quotes as shown we can still fetch the same data.

select * from test_schema1.students where marks > '450';
select * from test_schema1.students where marks = ‘250’;

The following operators can be used in the WHERE clause:

OperatorDescriptionExample
=Equalsselect * from students where name = 'KMS';
>Greater thanselect * from students where marks > 450;
<Less Thanselect * from students where marks < 450;
>=Greater than or equalselect * from students where marks >= 450;
<=Less than or equalselect * from students where marks <= 450;
<>Not equal (we can also use != in some versions )select * from students where marks <> 450;
select * from students where marks != 450;
BETWEENbetween the range including the low and high valuesselect * from students where marks between 250 and 400;
LIKEis like pattern provided 1. Name having ‘k’
select * from students where name like '%k%';

2. Name starting with ‘k’
select * from students where name like 'k%';

3. Name ending with ‘k’
select * from students where name like '%k';
INfor multiple column valuesselect * from students where marks in (390, 250, 400, 450);

-A blog by Shwetali Khambe

Related Posts