SQL Not EqualYou 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:
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 EqualThe 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 OperatorLet us use different examples to implement the Not Equal operator on the Student table in different scenarios. The Student table contains the following records:
Implement the below SQL query to create the above table: 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. 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. Multiple Conditions Using Not Equal OperatorThe 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. Implementing SQL Not operator and SQL Group By clauseThe 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:
Implement the below SQL query to add above table in your database. Now lets use the GROUP BY and HAVING clause with Not Equal Operator on the table. ConclusionThrough 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. Next TopicDATEDIFF() SQL Function |