The "WHERE" clause in SQL is used in conjunction with the "SELECT", "UPDATE", "DELETE", and other SQL statements to filter rows based on specified conditions.
It allows us to retrieve, update, or delete only those rows that meet the specified criteria.
WHERE clause In SQL can be used with different types of comparison operators such as =(Equal Operator), !=(Not Equal), <(less than Operator), >(Greater than Operator), BETWEEN, IN, LIKE and more.
Here is the basic syntax of the WHERE clause:
SELECT columnName1, columnName2, FROM tableName WHERE condition;
columnName1, columnName2 : The columns you want to retrieve from the table. You can use an asterisk `*` to select all columns.
tableName: The name of the table from which you want to retrieve data.
WHERE condition: The condition that specifies which rows to include in the result set or include rows which matches the conditions.
This query retrieves all columns from the products table where the category is equal to 'GAMES'.
SELECT * FROM products WHERE category = 'GAMES';
This query retrieves all columns from the products table where the price is greater than 1000.
SELECT * FROM products WHERE price > 1000;
This query retrieves all columns from the products table where the price is less than 1000.
SELECT * FROM products WHERE price < 1000;
Retrieves all products whose category not equal to 'GAMES'.
SELECT * FROM products WHERE category != 'GAMES';
Retrieves all products whose price is between 500 to 1000.
SELECT * FROM products WHERE products BETWEEN 500 AND 1000;
Retrieves all products in the 'GAMES' or 'CLOTHING' category.
SELECT * FROM products WHERE category IN ('GAMES', 'CLOTHING');
Retrieves all products whose productName starts with 'pu'.
Retrieves all products whose productName ends with 'bg'.
// Start with "pu" SELECT * FROM products WHERE products LIKE 'pu%'; // End with "bg" SELECT * FROM products WHERE products LIKE '%bg';