The WHERE Clause
The WHERE clause filters records based on conditions.
Basic Syntax
sql
1SELECT columns
2FROM table_name
3WHERE condition;Filter by Exact Match
Find all students enrolled in Python:
sql
1SELECT * FROM students
2WHERE course = 'Python';Result
| id | name | age | course | marks |
|---|---|---|---|---|
| 1 | Alice | 14 | Python | 85 |
| 3 | Carol | 15 | Python | 92 |
Comparison with Equals
The = operator checks for exact matches:
sql
1-- Filter by course
2SELECT * FROM students WHERE course = 'Python';
3
4-- Filter by grade
5SELECT * FROM students WHERE grade = 'A';
6
7-- Filter by age
8SELECT * FROM students WHERE age = 14;String vs Number Comparisons
sql
1-- Strings need quotes
2SELECT * FROM students WHERE name = 'Alice';
3
4-- Numbers don't need quotes
5SELECT * FROM students WHERE age = 14;
6
7-- But this also works (not recommended)
8SELECT * FROM students WHERE age = '14';Case Sensitivity
Depending on your database:
sql
1-- May or may not match 'Python'
2SELECT * FROM students WHERE course = 'python';- MySQL: Case-insensitive by default
- PostgreSQL: Case-sensitive
- SQLite: Case-insensitive for ASCII
Key Points
- WHERE filters rows before returning results
- Use
=for exact matches - Strings require quotes, numbers don't
- Case sensitivity depends on database
