Careeroza
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
| CustomerID | CustomerName | ContactName | City | Country |
|---|---|---|---|---|
| 1 | Aarav Traders | Aarav Shah | Mumbai | India |
| 4 | Delta Mart | Priya Nair | Bengaluru | India |
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
| ProductID | ProductName | Category | Price | Stock |
|---|---|---|---|---|
| 1 | Basmati Rice 5kg | Grocery | 12.5 | 120 |
| 3 | Olive Oil 1L | Grocery | 9.99 | 80 |
| 5 | Espresso Beans 1kg | Beverages | 18 | 45 |
Operators in The WHERE Clause
| Operator | Description |
|---|---|
= | Equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
<> or != | Not equal |
BETWEEN | Between a certain range |
LIKE | Search for a pattern |
IN | Match any value in a list |
IS NULL | Value is missing |
Example
Employees not in the Sales department:
SELECT FirstName, Department FROM Employees
WHERE Department <> 'Sales';Result: 3 rows
| FirstName | Department |
|---|---|
| Ananya | Support |
| James | Support |
| Fatima | Finance |
Exercise
Exercise: Select all orders with an Amount of 200 or more.
Show answer
SELECT * FROM Orders
WHERE Amount >= 200;