What is the meaning of <> in MySQL query?


The symbol <> in MySQL is same as not equal to operator (!=). Both gives the result in boolean or tinyint(1). If the condition becomes true, then the result will be 1 otherwise 0.

Case 1 − Using != operator.

The query is as follows −

mysql> select 3!=5;

The following is the output.

+------+
| 3!=5 |
+------+
| 1    |
+------+
1 row in set (0.00 sec)

Case 2 − Using <> operator.

The query is as follows −

mysql> select 3 <> 5;

The following is the output.

+--------+
| 3 <> 5 |
+--------+
| 1      |
+--------+
1 row in set (0.00 sec)

The <> operator can be used to return a set of rows from the table. The <> is a standard ANSI SQL.

Let us first create a table. The query to create a table is as follows −

mysql> create table NotEqualOperator
-> (
-> StudentId int,
-> StudentName varchar(100),
-> StudentSection varchar(10)
-> );
Query OK, 0 rows affected (0.78 sec)

Insert some records in the table using insert command. The query is as follows.

mysql> insert into NotEqualOperator values(1,'John','A');
Query OK, 1 row affected (0.19 sec)

mysql> insert into NotEqualOperator values(2,'Carol','B');
Query OK, 1 row affected (0.19 sec)

mysql> insert into NotEqualOperator values(3,'Sam','A');
Query OK, 1 row affected (0.15 sec)

mysql> insert into NotEqualOperator values(4,'Mike','B');
Query OK, 1 row affected (0.23 sec)

mysql> insert into NotEqualOperator values(5,'Bob','B');
Query OK, 1 row affected (0.19 sec)

mysql> insert into NotEqualOperator values(6,'David','B');
Query OK, 1 row affected (0.14 sec)

mysql> insert into NotEqualOperator values(7,'Ramit','A');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement. The query is as follows.

mysql> select *from NotEqualOperator;

The following is the output.

+-----------+-------------+----------------+
| StudentId | StudentName | StudentSection |
+-----------+-------------+----------------+
| 1         | John        | A              |
| 2         | Carol       | B              |
| 3         | Sam         | A              |
| 4         | Mike        | B              |
| 5         | Bob         | B              |
| 6         | David       | B              |  
| 7         | Ramit       | A              |
+-----------+-------------+----------------+
7 rows in set (0.00 sec)

As discussed above, the <> operator can be used to return a set of rows. Now filter the above table to get those students only that does not belong to section A.

The query is as follows.

mysql> select *from NotEqualOperator where StudentSection <>'A';

The following is the output.

+-----------+-------------+----------------+
| StudentId | StudentName | StudentSection |
+-----------+-------------+----------------+
| 2         | Carol       | B              |
| 4         | Mike        | B              |
| 5         | Bob         | B              |
| 6         | David       | B              |
+-----------+-------------+----------------+
4 rows in set (0.00 sec)

Updated on: 25-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements