Comparison Operators: Equals and Not Equals
You'll learn how to filter SQL query results to include only rows that match—or don't match—a specific value.
The Core Idea
When working with databases, you rarely want all the data. Most of the time, you're looking for specific records: customers from a particular city, orders with a certain status, or products that aren't discontinued. SQL's comparison operators let you do exactly that by filtering rows based on exact matches or exclusions.
The two fundamental comparison operators are:
=(equals): finds rows where a column matches a specific value!=or<>(not equals): finds rows where a column doesn't match a value
How They Work
Think of these operators like a bouncer at a club checking IDs. The equals operator (=) only lets through records that exactly match your criteria. The not-equals operator (!= or <>) does the opposite—it lets through everything except the value you specify.
Using Equals (=)
SELECT * FROM customers
WHERE country = 'USA';
This retrieves only customers whose country is exactly 'USA'.
Using Not Equals (!= or <>)
SELECT * FROM orders
WHERE status != 'Cancelled';
This retrieves all orders except those with 'Cancelled' status. You can also write it as:
WHERE status <> 'Cancelled'
Both != and <> work identically—they're just different syntax styles.
Practical Context
These operators work in the WHERE clause you learned earlier, turning it from a concept into a powerful filtering tool. Use = when you want precision, and != when you want exclusion.
Key Takeaway: Use = to filter for exact matches and != (or <>) to exclude specific values from your SQL query results.