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:

Operator Description Example
= Equals select * from students where name = 'KMS';
> Greater than select * from students where marks > 450;
< Less Than select * from students where marks < 450;
>= Greater than or equal select * from students where marks >= 450;
<= Less than or equal select * 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;
BETWEEN between the range including the low and high values select * from students where marks between 250 and 400;
LIKE is 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';
IN for multiple column values select * from students where marks in (390, 250, 400, 450);

-A blog by Shwetali Khambe

Related Posts