Lesson 15: Filtering Data with WHERE Clauses
Retrieving an entire table is rarely useful. The WHERE clause allows you to filter the results based on specific conditions.
Basic Syntax with WHERE
sql SELECT [columns] FROM [table] WHERE [condition];
Comparison Operators
We use standard comparison operators within the WHERE clause:
| Operator | Meaning | Example |
|---|---|---|
= | Equals | WHERE Age = 30 |
> | Greater than | WHERE Price > 50 |
< | Less than | WHERE Quantity < 10 |
>= | Greater than or equal to | WHERE Sales >= 1000 |
!= or <> | Not equal to | WHERE Country <> 'USA' |
Example 1: Filtering Numeric Data
Find all employees who earn more than 50,000.
sql SELECT Name, Salary FROM Employees WHERE Salary > 50000;
Example 2: Filtering Text Data
String values must be enclosed in single quotes (').
Find all products in the 'Electronics' category.
sql SELECT Name, Price FROM Products WHERE Category = 'Electronics';