Javatpoint Logo
Javatpoint Logo

SQL Not Equal

You must have used the comparison operators in mathematics to compares two numbers or expression in your classes. These operators allows the user to perform comparison based on the conditions.

For example if we want to compare sales of two cars that can be done by comparing the annual sales of the cars. If CAR A sales is 10,000 while the sales for CAR B is 23,000. then we can draw the conclusion that CAR B is more popular than CAR A based on the comparison of their respective sales.

Some of the comparison operators that can be implemented in SQL statements are as follows:

Operator Description
= Equals to
<> Not Equal
!= Not Equal
> Greater than
>= Greater than to equals to
< Less than
<= Less than or equals to

You can see in the tables that two different operators have the same description as Not Equa. These operators are <> and !=. In this tutorial we will discuss both these operators and their functions and differences.

SQL Not Equal Comparison Operator: <>

The user can implement the SQL Not Equal comparison operator to compare two expressions in SQL. For instance, 5<>6 will compare 5 and 6 by using the not equal comparison operator in between the two values. The user can also use an expression to compare instead of the values.

Difference between SQL Not Equal Operator <> and !=

The user can perform the inequality test on two values or expression by using either of the two not equal operators (<>, !=). SQL provide support for both the operators and perform the same functions. The user can implement either of these operators and will finally reach the same conclusions. There is only one difference in these operators and that is <> is support of ISO standard while the != does not follow the ISO standards. Therefore, it is advised to use <> operator especially when implementing in an application as it follows ISO standard.

SQL Not Equal Operator: !=

This is non standardized Not Equal operator, it performs not equal operation on the expressions, the operator will return 1 if the values are not equal and it will return 0 if the values are equal. The operator will return a NULL value if either of the values in statement is NULL. If the expression used for comparison return a different types of data upon manipulation, it performs type conversions to return the result.

Let us discuss an example to see the implementation and result from implementing above mentioned operators.

For example,

15 != 17 Or 15 <> 17 will return 1 that is TRUE

13 != 13 Or 13 <> 13 will return 0 that is FALSE

20 != NULL Or 20 <> NULL will return NULL as result.

Note: "!=" and "<>" both will give the same results.

Implementing Not Equal Operators in SQL

Example:

The above query will return the same results on implementation. It will return all the records where the data value for name is not AMAN.

The Return Value Of SQL Not Equal

The value returned by the implementation of the Not Equal operator is Boolean type. Thus, Not Equal is a boolean expression. It can either return TRUE or FALSE based on the comparison of the values or expressions in the statement.

Example:

1 != 2 -----> True

3 != 3 -----> False

Implementing Not Equal Operator

Let us use different examples to implement the Not Equal operator on the Student table in different scenarios.

The Student table contains the following records:

S_ID S_Name S_Course S_City Phn_No
1 Aman English Noida 792403929
2 Harsh Hindi Ghaziabad 832403929
3 Raghav Maths Delhi 992403953
4 Raman Computer Science Jaipur 345903976
5 Seema Maths Kanpur 992893909

Implement the below SQL query to create the above table:


SQL Not Equal

Example 1: Implement the below SQL query to retrieve all the records from the Student table except where S_ID is 1.

The data retrieved after implementing the above query will exclude the details of student with S_ID = 1.

SQL Not Equal

Example 2: Another instance to retrieve the list of all the students except one specific student.

The above example will exclude the details of the customer whose S_Name is Harsh.

SQL Not Equal

Multiple Conditions Using Not Equal Operator

The user can implement multiple conditions using the Not Equal Operator in SQL. The user can specify the conditions in the WHERE clause of the SQL query.

Let see an example to implement multiple conditions while using Not Operator.

The statement given above will produce the following results.

SQL Not Equal

Implementing SQL Not operator and SQL Group By clause

The user can also use the SQL Not Operator while using the GROUP BY and HAVING clause. Lets consider a query to understand the implementation of the Not operator with Group By clause.

To perform the above operation lets start with creating a new table. The table is as follows:

Year College Registration
2020 Engineering 1500
2021 Engineering 2000
2022 Engineering 2100
2020 Management 800
2021 Management 1400
2022 Management 1300

Implement the below SQL query to add above table in your database.


SQL Not Equal

Now lets use the GROUP BY and HAVING clause with Not Equal Operator on the table.


SQL Not Equal

Conclusion

Through this article, you have now gained a solid understanding of SQL Not equal Operator, along with pertinent examples. Equality operator improves the performance of the SQL query.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA