r/SQL • u/marius_siuram • Oct 28 '26
Discussion Language-lawyer: != vs <>
Almost every SQL DBMS I can think of accept != as the "not equal operator". However, I realized that W3Schools only shows <>.
I was under the impression that <> is "the good-ol ANSI standard one" and every implementation out there was allowing the use of !=. But somebody was correcting me and saying that it has been on-spec for a while. I don't own the ANSI (a bit pricey for a language-lawyer curiosity!). Was I wrong?
Edit: After seeing quite a concensus on these answers, I talked with John (the one correcting me before). We checked the 2023 standard and his confusion came from the use of != as an alternative not equals operator for SQL/JSON lexical elements. That means that inside SQL/JSON blocks you may use != but on regular SQL (e.g. in the plain WHERE) using != is not covered by the standard (even if it is widely accepted for modern engines). It was a "coffee discussion", not something relevant IRL, but we both wanted to know the truth behind it.
17
u/gumnos Oct 28 '26
hah, I've wondered this myself…
I tend to use
!=because I'm an old-school dev who cut teeth on C programming ,but I see both, and get the confusion.13
u/whodiopolis Oct 28 '26
You are correct. <> is in the standard, and != is not. Oracle also allows up carrot and = (
^=) which I have not seen used seriously. Just be mindful some rdbms will parse each operator separately and some will parse all not equal operators as if they were the same symbol. Just means there's a non zero performance difference if you mix them in certain systems.9
u/ThetaLife Oct 28 '26
It doesn't matter which you use as long as you and your team all do it the same way.
5
u/Trasvi89 Oct 28 '26
Damn ive been doing different variants of sql for 15 years and only ever used <>
5
u/markwdb3 When in doubt, test it out. Oct 28 '26 edited Oct 28 '26
First, I'll say I've seen many discussions or debates along these lines: "Oh no, I don't want to use x in my query - it is not standard SQL." Or: "It is ok to use x, because it's standard SQL." Usually folks making such statements don't actually know what is and what is not standard. And we can't blame them too much, because as you say, these ANSI/ISO documents are not freely available. You have to search around, perhaps find an old leaked document, or trust some third-party source that references or directly quotes ANSI/ISO, such as the Postgres documentation or what have you.
For example, recently I saw on LinkedIn a poster espousing the virtues of
IS DISTINCT FROM. A commenter wrote that he doesn't like it, because it's not standard SQL. OP said that may be so, but if you're using a dialect that supports it, it's handy.They were both wrong - not about the virtues of
IS DISTINCT FROMin and of itself, but on whether it is standard. I let them know that it actually IS part of standard SQL. Source: on this page you can find where Lukas Eder (the man behind jOOQ) quotes the syntax diagram from SQL:1999, and that's good enough for me - https://blog.jooq.org/the-is-distinct-from-predicate/ (Unfortunately his SQL:1999 link is now dead.)Now here's the thing - just because some syntax or feature or whatever is standard SQL, doesn't mean it's supported by all implementations. So, if portability is the real concern, rather than purity of standard conformance, maybe it's better to look into how widely x is actually supported. Regarding
IS DISTINCT FROM, modern-sql.com would not be a bad place to start: https://modern-sql.com/caniuse/is-distinct-fromAs for
!=, I cannot find it as an operator in any of the following docs, which all define "not equals operator" as<>(this person seems to have captured the BNF grammar for some parts of some versions):https://ronsavage.github.io/SQL/sql-92.bnf.html
https://ronsavage.github.io/SQL/sql-99.bnf.html
https://ronsavage.github.io/SQL/sql-2003-2.bnf.html
The last doc is 2003. If the claim is the addition of
!=is a newer development than 2003, then the question remains unanswered, at least by this comment. Is it worth digging any deeper? I don't know.My general stance is just use x, if it's supported by your SQL dialect and adds value. If you're not sure if it's standard and it does not add value, and there's a definite standard solution, use that instead. So, I prefer to just use
<>everywhere over!=. But I feel nitpicky if I call out a coworker using!=without a problem. It's certainly implemented in many SQL engines, and if you have to port hand-written SQL one day, to a rare dialect that does not support!=, this is likely to be one of the least difficult conversions you'll have to make.OK, my two cents are in.
1
u/ArielCoding 29d ago
The ¡= found isn’t part of normal SQL, it belongs to the JSON path language, and it borrows JavaScript habits, so you were both kind of right.
2
u/phesago Oct 28 '26
If you run 2 queries using each with execution plans on, you see that both compile as ‘<>’
2
u/jacquesrk oracle Oct 28 '26
All I know is that Oracle SQL allows the following
and I'm pretty sure used to allow
but that last one isn't mentioned in the documentation any more - shown in Oracle 12.2 docs
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Comparison-Conditions.html#GUID-828576BF-E606-4EA6-B94B-BFF48B67F927
not shown in oracle 18 docs
https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/Comparison-Conditions.html#GUID-828576BF-E606-4EA6-B94B-BFF48B67F927
1
u/MrRoy_SQL 29d ago
nice follow up, didn't know about the SQL/JSON bit. practically i just stick with <> since it's the standard one and works everywhere, and != is fine if your team's codebase already uses it. consistency matters more than which one
1
u/Either-Surprise6390 28d ago
I think I've always used <> . My original college textbook was a really old one though...
1
u/phasmaglass 28d ago
lmfao, just skimming past this post straight up triggered tf out of me because I am constantly having to write MSSQL stored procedures and C#, that's like 90% of my job, then every once in awhile I have to use a proprietary tool that can't parse full SQL, we have a weird custom SQL Lite implementation for it, and every single time if I have to filter data in that tool, I will end up with a != breaking everything. Then after I've wasted a bunch of time debugging this my brain "overlearns" the lesson and starts writing C# wrong. To live is to suffer
1
u/Empty-Donut-6838 25d ago
i always stick with the diamond brackets because breaking an obscure driver is my worst nightmare. muscle memory from c makes me type the exclamation point every single tim though
1
u/TravelingSpermBanker Oct 28 '26
I literally use both, depending on what is currently being used, so that the format stays consistent.
If I was doing something from scratch, I think I’d do != now because it’s more intuitive. I learned <> first I do believe
1
u/my_password_is______ 29d ago
<> reminds me of VBA
so I prefer to use != which reminds me of C