- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - NOT EQUAL Operator
The SQL NOT EQUAL Operator
The SQL NOT EQUAL operator is used to compare two values and return TRUE if they are not the same. It is represented by "<>" and "!=". The difference between these two is that <> follows the ISO standard, but != doesn't. So, it is recommended to use <>.
The NOT EQUAL operator is often used in the WHERE clause to filter rows that do not match a given value, and it can also be used in the GROUP BY clause to group results.
When using the NOT EQUAL operator (<> or !=) with text values, the comparison is case-sensitive by default unless the database is configured.
Syntax
Following is the standard SQL syntax for the NOT EQUAL operator using the ANSI standard symbol (<>):
SELECT * FROM table_name WHERE column_name <> value;
Alternatively, many SQL databases also support the NOT EQUAL operator using the symbol (!=):
SELECT * FROM table_name WHERE column_name != value;
Example
To understand it better let us consider the CUSTOMERS table which contains the personal details of customers including their name, age, address and salary etc. as shown below:
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
The table will be created as follows:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
SQL NOT EQUAL with Text
We can use the NOT EQUAL operator with text in SQL to compare two text values and return. We can use "<>" or "!=" in the WHERE clause of a SQL statement and exclude rows that match a specific text value.
Example
In the following query, we are retrieving all the records from the CUSTOMERS table whose NAME is not 'Ramesh':
SELECT * FROM CUSTOMERS WHERE NAME <> 'Ramesh';
The output of the above code is as shown below:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
SQL NOT EQUAL with GROUP BY Clause
We can use the NOT EQUAL operator with the GROUP BY clause to group the results by the values that are not equal to the specified text value.
The aggregate functions such as COUNT(), MAX(), MIN(), SUM(), and AVG() are frequently used with the GROUP BY statement.
Example
Here, we are retrieving the number of records with distinct ages (excluding '22') in the 'CUSTOMERS' table and grouping them by age value:
SELECT COUNT(ID), AGE FROM CUSTOMERS WHERE AGE <> '22' GROUP BY AGE;
On executing the above query, it will generate the output as shown below:
| COUNT(id) | AGE |
|---|---|
| 1 | 32 |
| 2 | 25 |
| 1 | 23 |
| 1 | 27 |
| 1 | 24 |
SQL NOT EQUAL with Multiple Conditions
The not equal operator can also be used with multiple conditions in a WHERE clause to filter out rows that match specific criteria.
Example
Now, we are retrieving all the customers whose salary is either ">2000" or "=2000". At the same time, the customer must not be from "Bhopal":
SELECT * FROM CUSTOMERS WHERE ADDRESS <> 'Bhopal' AND (SALARY>'2000' OR SALARY='2000');
Following is the output of the above code:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Negating a Condition Using SQL NOT EQUAL
In SQL, the NOT EQUAL operator can also be combined with the NOT Operator to negate a condition. It filters out the rows that meet a specific condition.
Example
In the following query, we are retrieving all rows from the "CUSTOMERS" table where the "SALARY" is equal to '2000':
SELECT * FROM CUSTOMERS WHERE NOT SALARY != '2000';
After executing the above code, we get the following output:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
Important Points About SQL NOT EQUAL Operator
Following are some of the important points we should know about the SQL NOT EQUAL Operator:
- The SQL NOT EQUAL operator is used to filter records where a column's value does not match the specified value.
- There are two common forms: the ANSI standard syntax <> and the alternative syntax !=. Both work the same way in most databases.
- Some older SQL databases may not support !=, so using <> is safer for cross-database compatibility.
- The comparison is case-sensitive or case-insensitive depending on the database collation settings.
- When comparing with NULL, the NOT EQUAL operator will not return TRUE because NULL represents unknown. Use IS NOT NULL instead.
- It can be combined with other logical operators such as AND, OR, and NOT to create complex filter conditions.
- Performance may depend on indexing and query optimization, especially when filtering large datasets.