SQL – Difference between != and <> Operator used for NOT EQUAL TO Operation

Here is interesting question received on my Facebook page. (On a side note, today we have crossed over 50,000 fans on SQLAuthority Facebook Fan Page).

What is the difference between != and <>Operator in SQL Server as both of them works same for Not Equal To Operator? 

SQL - Difference between != and <> Operator used for NOT EQUAL TO Operation Not-Equal-To-Operator

Very interesting question indeed. Even though this looks very simple when I asked quite a few people if they know the answer before I decided to blog about it. The answer which I received was that it seems that many know the answer but everybody wanted to know the more about it.

Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.

Here is the follow up question I received right I answer that there is no difference between those operator.

If != and <> both are the same, which one should be used in SQL queries?

Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.

Though, many of the leading database applications supports both of the operators. For example –

  • SQL Server
  • MySQL
  • Oracle
  • SQLite
  • Sybase
  • IBM Informix
  • PostgreSQL

Here is my return question to you which one of the following operators you use for NOT EQUAL TO operation?

  1. !=
  2. <>

Please leave your answer with reason in comment field and I will publish the interesting answer as a follow up blog post with due credit.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Previous Post
SQL SERVER – Check Database Level (IsNullConcat) and Session Level Settings (CONCAT_NULL_YIELDS_NULL) using T-SQL
Next Post
SQL – Download Database Cheat Sheet for MongoDB, NuoDB, MySQL for FREE

Related Posts

No results found.

55 Comments. Leave new

  • I change employers often enough to want to match standards accepted across platforms, APIs, and applications. Since the ” operator is in the SQL-92 Standard and ANSI compliance, I use the ” operator. In “C”, I used ‘!=’, but starting with C++, ” was the accepted norm. C# uses ‘!=’, so when writing embedded SQL in C#, I match the host standard, ‘!=’.

    Reply
  • Steve Delaney
    April 14, 2017 9:34 pm

    I use because the keys are right next to each other and both need the shift state. It’s easier!

    Reply
  • Is there any diffence between using the operator > and the operator >=, in terms of performance?

    Example: WHERE date > ‘2014-12-31’ | WHERE date >= ‘2015-01-01’

    Thanks in advance!
    Nuno

    Reply
    • I would love to know the answer to this one too. Great question.

      Reply
      • I don’t think there is any performance difference unless you have a partitioned table. If you have a table partitioned by month it is possible that one of those would trigger partition pruning. It will depend on what variant of SQL you are using and what version. It is more common in older SQL versions for the where clause to match the partition range exactly in order to prune unneeded partitions. So if you have a partition with a range of 2015-01-01 to 2015-02-01, date >= 2015-01-01 should always prune your partitioned table where date > 2014-12-31 may not.

    • Results could be different if data is DateTime
      E.g 2014-12-31 13:30:30

      Reply
  • I’ll use operator “” as this is supported by the SQL standard and not dependent for a particular vendor (like SQL Server,Oracle etc) so as a result this operator can be used across all the databases

    Reply
  • I usually use the because it is the first symbol I learned for NOT EQUAL TO when I started programing. However, there are some programming languages where only the != symbol is recognized and I am forced to adapt.

    Reply
  • Upendra Patil
    March 15, 2020 1:34 am

    I have been using for long time (almost 24 years). However, if you are using and script deployment tool(e.g. liquibase), are not allowed because they are HTML tags. So I had to switch to !=.

    Reply

Leave a Reply

Menu