Not Equal Operator
To find records that DON'T match a value, use != or <>.
Syntax
sql
1-- Using !=
2SELECT * FROM students WHERE course != 'Python';
3
4-- Using <> (ANSI standard)
5SELECT * FROM students WHERE course <> 'Python';Both operators work the same way.
Example: Non-Python Students
sql
1SELECT * FROM students
2WHERE course != 'Python';Result
| id | name | age | course | marks |
|---|---|---|---|---|
| 2 | Bob | 12 | JavaScript | 78 |
| 4 | David | 13 | Ruby | 65 |
| 6 | Frank | 14 | HTML | 71 |
More Examples
sql
1-- Students not in grade A
2SELECT * FROM students WHERE grade != 'A';
3
4-- Students not aged 12
5SELECT * FROM students WHERE age != 12;
6
7-- Students with marks not equal to 85
8SELECT * FROM students WHERE marks <> 85;!= vs <>
| Operator | Description |
|---|---|
!= | Common, widely supported |
<> | ANSI SQL standard |
Both are correct. Choose one and be consistent.
Important Note
Not equal does NOT match NULL values:
sql
1-- This won't return rows where course IS NULL
2SELECT * FROM students WHERE course != 'Python';To include NULLs, use IS NULL separately (covered later).
