MySQL WHERE Clause

MySQL WHERE Clause

MySQL WHERE Clause is used with SELECT, INSERT, UPDATE and DELETE clause to filter the results. It specifies a specific position where you have to do the operation.

Syntax:

  1. WHERE conditions;

Parameter:

conditions: It specifies the conditions that must be fulfilled for records to be selected.

MySQL WHERE Clause with single condition

Let’s take an example to retrieve data from a table “officers”.

Table structure:

whe

Execute this query:

  1. SELECT *
  2. FROM officers
  3. WHERE address = ‘Mau’;

Output:

where

MySQL WHERE Clause with AND condition

In this example, we are retrieving data from the table “officers” with AND condition.

Execute the following query:

  1. SELECT *
  2. FROM officers
  3. WHERE address = ‘Lucknow’
  4. AND officer_id < 5;

Output:

new

WHERE Clause with OR condition

Execute the following query:

  1. SELECT *
  2. FROM officers
  3. WHERE address = ‘Lucknow’
  4. OR address = ‘Mau’;

Output:

new1

MySQL WHERE Clause with combination of AND & OR conditions

You can also use the AND & OR conditions altogether with the WHERE clause.

See this example:

Execute the following query:

  1. SELECT *
  2. FROM officers
  3. WHERE (address = ‘Mau’ AND officer_name = ‘Ajeet’)
  4. OR (officer_id < 5);

Output:

new2