Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
To filter rows effectively in a WHERE clause, you evaluate expressions using Comparison Operators. These operators compare two expressions and evaluate to TRUE, FALSE, or UNKNOWN.
| Operator | Meaning | Example | Result for Match |
|---|---|---|---|
= | Equal to | status = 'Shipped' | TRUE if values match. |
!= or <> | Not equal to | department != 'HR' | TRUE if values differ. |
< | Less than | price < 500.00 | TRUE if left is strictly smaller. |
<= | Less than or equal to | age <= 60 | TRUE if left is smaller or equal. |
> | Greater than | salary > 50000.00 | TRUE if left is strictly larger. |
>= | Greater than or equal to | rating >= 4.5 | TRUE if left is larger or equal. |
!= vs <>: Which is Preferred?In MySQL, both != and <> perform the exact same "not equal to" comparison.
<> is the official ANSI SQL standard operator, compatible across all database vendors (Oracle, PostgreSQL, SQL Server, SQLite).!= is a popular vendor convenience operator.<> for maximum cross-database portability.One of the most common bugs in SQL query writing involves combining not-equal operators with NULL values:
Question: Will the employee with department = NULL be returned? Answer: NO! Because NULL <> 'HR' evaluates to UNKNOWN, the row is silently discarded! If you want to include employees without a department, you must write:
<=>)To solve the NULL comparison problem, MySQL provides a unique operator: the NULL-Safe Equal Operator (<=>), affectionately called the Spaceship Operator:
You can use the spaceship operator to safely compare columns that might contain NULL without writing cumbersome OR col IS NULL clauses:
col = NULL: In SQL, writing WHERE status = NULL will never return any rows, because nothing can equal an unknown! Always use IS NULL or the spaceship operator <=>.name = 'amit' will match 'Amit' and 'AMIT' if your collation is case-insensitive (_ci).SELECT 5 <=> NULL; evaluate to in MySQL?SELECT * FROM users WHERE middle_name = NULL; return 0 rows even if users have NULL middle names?<=>) return when comparing NULL <=> NULL?status = 'Active', 2 with status = 'Banned', and 1 with status = NULL), how many rows will WHERE status <> 'Banned' return?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Filtering Rows with WHERE Clause | Logical Operators: AND, OR, NOT |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.