WHERE clause in MySQL

MySQL WHERE
In MySQL, the WHERE clause is used to filter the results obtained through the SELECT, INSERT, UPDATE, and DELETE statements.

Syntax:

WHERE conditions;

Parameters:

conditions: It is used to specify the conditions to be strictly followed for selection.

Example: Selecting specific fields from a table. Items table:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Query:

SELECT *  
FROM items  
WHERE  id < 4;

Output:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100

 

Example: Using WHERE clause with AND condition. Items table:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Query:

SELECT *  
FROM items  
WHERE  id < 4 AND id > 1;

Output:

ID NAME QUANTITY
2 Sports 45
3 Fashion 100

 

Example: Using WHERE clause with OR condition. Items table:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Query:

SELECT *  
FROM items  
WHERE  id < 4 OR id > 1;

Output:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Example: Using WHERE clause with both AND and OR conditions. Items table:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Query:

SELECT *  
FROM items  
WHERE  (id < 4 AND quantity > 50)
OR id > 3;

Output:

ID NAME QUANTITY
3 Fashion 100
4 Grocery 90
5 Toys 50