The SQL not equal operator is a comparison operator that is used to find rows in a relational database. In this article, we’ll take a look at how it’s used.
Let’s take a look at a typical query in SQL:
SELECT * from names WHERE ...stuff goes here
The SELECT looks at column names from your specified table (in this instance, it’s names). The 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 very similar to how other programming languages represent the not equal clause.
Here is a working example that creates a table called names, adds some people to it, and then queries the data with the not equal symbol:
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;
As you can see I used both the <> and the != to show that both work in this instance – I’m using sqlite3 in a repl.it sandbox to create this context. Your experience may vary if you are using a different code editor or sql sandbox environment.
The results should show as follows:
2|Ashleigh 3|Sarah 4|Tad 5|Dustin 6|Elissa
You did it! That’s how you use the Not Equal operator in SQL.