The SQL not equal operator is represented by <>. This operator lets you select rows from a database that do not meet a particular condition. The != operator is used in a WHERE statement. The != operator can be used instead of <> in some instances of SQL.
How to Use the SQL Not Equal Operator
Have you ever wanted to select rows where a field’s contents are not equal to a particular value? You’re in luck because SQL has an operator built for this purpose.
You can select rows based on whether one or more of their fields’ values is not equal to a particular value.
In this guide, we’re going to talk about using the SQL not equal operator. We’ll refer to an example to help you get started.
SQL Not Equal
The SQL not equal operator is <>. You should specify this in a WHERE statement. This lets you select rows where a particular column’s contents is not equal to the value you have specified. You can also use != as a not equal statement in some versions of SQL.
Let’s take a look at a typical query in SQL:
SELECT * from names WHERE ...stuff goes here
The SQL SELECT statement looks at column names from your specified table (in this instance, it’s names). The SQL WHERE clause is where our conditional is going to go for the not equal statement.
In traditional ISO standard SQL, the symbol that represents not equal is <>. In other versions, you can use !=. If you’re unsure which to use, stick with the standard since that for sure works. Think the bang equal was created because it’s similar to how other programming languages represent the not equal clause.
Not Equal SQL Example
We’re going to create a table called “names”. We’re going to add some records to that table. We will then use the not equal symbol to query data from the table:
CREATE TABLE names ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL ); INSERT into names VALUES (1, "Christina"); INSERT into names VALUES (2, "Ashleigh"); INSERT into names VALUES (3, "Sarah"); INSERT into names VALUES (4, "Tad"); INSERT into names VALUES (5, "Dustin"); INSERT into names VALUES (6, "Elissa"); INSERT into names VALUES (7, "Kelly"); SELECT * from names WHERE name <> "Christina" AND id != 7;
I used both the <> and the != to show that both work in this instance. I’m using sqlite3 in a Repl.it sandbox to host this database. Your experience may vary if you are using a different code editor or SQL sandbox environment.
The result set from our SQL statement shows:
2|Ashleigh 3|Sarah 4|Tad 5|Dustin 6|Elissa
Our query returns all the records where “name” is not equal to “Christina”. The “id” must not equal to 7 for a record to be returned. You did it! That’s how you use the not equal to comparison operator in SQL.
Conclusion
You can select rows from a database whose contents do not match a particular value using the SQL not equal operator. In most versions of SQL, the not equal operator is a less than followed by a greater than sign (<>).
An exclamation mark followed by an equals sign (!=) is available as a not equal operator in some versions of SQL. This statement lets you compare two expressions to see whether they are not equal to each other.
Are you curious to learn more about SQL? Check out our complete How to Learn SQL guide. In this guide you’ll find expert advice on the top learning resources that you can use to advance your knowledge.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.