The SQL WHERE Clause is used to filter out the records. This Query allows you to add a condition that will return a resultset with that matching condition.
You can use the WHERE clause not only in SELECT statements but also you can use in UPDATE, DELETE, etc.
WHERE
SELECT
UPDATE
DELETE
SELECT column_1, column_2 FROM table_name WHERE condition;
Consider a customer table having multiples records
consider we want to select all the customers from the country “USA”, in the “Customers” table:
-- Filtering on Text field SELECT * FROM customers WHERE country = 'USA'; -- returns 2 records having country as `USA`
consider we want to select a customer whose ID is 2, in the “Customers” table:
-- Filtering on Numeric field SELECT * FROM customers WHERE id = 2; -- returns 1 records having id `2`
Other than equals to ( = ) we can use some more operators which are listed below:
=
>
Checks if the value is greater, than its return in result set.
SELECT * FROM customer WHERE sales > 20000;
<
Checks if the value is lesser, than its return in result set.
SELECT * FROM customer WHERE sales < 20000;
>=
Checks if the value is greater or equal, than its return in result set.
SELECT * FROM customer WHERE sales >= 12000;
<=
Checks if the value is lesser or equal, than its return in result set.
SELECT * FROM customer WHERE sales <= 12000;
<>
!=
Return all the records which are not equal to the define value.
SELECT * FROM customer WHERE sales <> 12000; -- or SELECT * FROM customer WHERE sales != 12000;
BETWEEN
Return all the records which are between a certain range
SELECT * FROM customer WHERE sales BETWEEN 10000 AND 20000;
LIKE
Return all the records which are matching, starting, or ending with a pattern.
SELECT * FROM customer WHERE customerName LIKE 'Yaya%';
IN
In where clause you can assign multiple value for a single field using IN.
SELECT * FROM customer WHERE country IN ('USA', 'Russia');