When writing a query, does it matter if I use <> or != when I mean "not equal to"?
As per the example below, nulls don't seem to be affected whether I use <> or !=. Is this an aesthetic thing?
declare @fruit table
(
FruitID int,
FruitName varchar(50)
)
insert into @fruit (FruitID, FruitName) Values (1, 'Apple')
insert into @fruit (FruitID, FruitName) Values (2, 'Orange')
insert into @fruit (FruitID, FruitName) Values (3, 'Pineapple')
insert into @fruit (FruitID, FruitName) Values (null, 'Not a Fruit')
select *
from @fruit
where fruitid<>2
select *
from @fruit
where fruitid!=2
The <>
operator is the operator defined by the formal sql standard. The !=
operator is an alias that is more familiar to many developers. They do the same thing, in the same way, and there are no performance or behavior differences on any dbms system I've worked with where both are supported.
Most database systems support !=
, but there are exceptions. Thus, in theory, <>
should be more portable, as it's the standard. In practice, I'd guess there are about as many that only implement !=
as there are that only implement <>
.
I come from a C++ background, and in my early days using SQL I didn't at first even know <>
existed. I learned about <>
not long after leaving school, when I had to spend some time working on a VB code base, and at the time thought it was just a quirk of the developers, that they would prefer the VB-style sql operator to go with their VB client code. For a long time after I still used !=
. Later I learned <>
was the standard, and had probably been used since '86, back when VB was still just "BASIC". I've since used <>
almost exclusively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With