Skip
Arish's avatar

6. Filtering with WHERE and Equals


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

idnameagecoursemarks
1Alice14Python85
3Carol15Python92

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

  1. WHERE filters rows before returning results
  2. Use = for exact matches
  3. Strings require quotes, numbers don't
  4. Case sensitivity depends on database