WHERE

The SQL WHERE Clause

The WHERE clause filters records. Only rows that meet the condition are returned (or updated, or deleted).

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: WHERE is also used in UPDATE and DELETE statements.

Text Values

Example

Select all customers from India:

SELECT * FROM Customers
WHERE Country = 'India';

Result: 2 rows

CustomerIDCustomerNameContactNameCityCountry
1Aarav TradersAarav ShahMumbaiIndia
4Delta MartPriya NairBengaluruIndia

SQL needs single quotes around text values. Most databases also accept double quotes, but in standard SQL double quotes are for identifiers (column/table names), so single quotes are the safe choice.

Numeric Values

Example

Numbers are written without quotes:

SELECT * FROM Products
WHERE Price > 9;

Result: 3 rows

ProductIDProductNameCategoryPriceStock
1Basmati Rice 5kgGrocery12.5120
3Olive Oil 1LGrocery9.9980
5Espresso Beans 1kgBeverages1845

Operators in The WHERE Clause

OperatorDescription
=Equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
<> or !=Not equal
BETWEENBetween a certain range
LIKESearch for a pattern
INMatch any value in a list
IS NULLValue is missing

Example

Employees not in the Sales department:

SELECT FirstName, Department FROM Employees
WHERE Department <> 'Sales';

Result: 3 rows

FirstNameDepartment
AnanyaSupport
JamesSupport
FatimaFinance

Exercise

Exercise: Select all orders with an Amount of 200 or more.

Show answer
SELECT * FROM Orders
WHERE Amount >= 200;