Course
NOT EQUAL Operator
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
NOT EQUAL Operator
MySQL NOT EQUAL Operator
The MySQL NOT EQUAL operator is used to compare two values and return true if they are not equal. 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 <> operator.
We can use this operator in WHERE clauses to filter records based on a specific condition and in GROUP BY clauses to group results.
Note: The comparison is case-sensitive by default when using this operator with text values.
Syntax
Following is the syntax of the NOT EQUAL operator in MySQL
SELECT column1, column2, ... FROM table_name WHERE column_name <> value;
Example
Firstly, let us create a table named CUSTOMERS using the following query
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));
The below query uses INSERT INTO statement to add 7 records into above-created table
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) 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 );
Execute the following query to retrieve all the records present in the CUSTOMERS table
SELECT * FROM CUSTOMERS;
Following is the CUSTOMERS table
NOT EQUAL with String Values
In MySQL, we can also use the NOT EQUAL to compare two string values. It returns true if both values are not equal. We can use "<>" or "!=" in the WHERE clause of a SQL statement and exclude rows that match a specific value.
Example
In the following query, we are selecting all the records from the CUSTOMERS table whose NAME is not "Khilan".
SELECT * FROM CUSTOMERS WHERE NAME <> "Khilan";
Output
The output of the above code is as shown below
NOT EQUAL with GROUP BY Clause
MySQL's NOT EQUAL operator can be used along with the GROUP BY clause. It will 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
In this query, we are counting the number of records with distinct 'ID' values for each 'AGE' in the 'CUSTOMERS' table. We are excluding records where 'AGE' is equal to '22', and grouping the results based on the 'AGE' column.
SELECT COUNT(ID), AGE FROM CUSTOMERS WHERE AGE <> '22' GROUP BY AGE;
Output
NOT EQUAL with Multiple Conditions
Depending on the situation, the NOT EQUAL operator can be used with multiple conditions in a WHERE clause to filter out rows that match specific criteria.
Example
Here, we are going to select 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');
Output
When we execute the query above, the output is obtained as follows
Negating a Condition Using NOT EQUAL
The MySQL NOT EQUAL operator can be combined with the NOT operator to negate a condition and filter out rows that meet a specific condition.
Example
The following query retrieves all rows from the "CUSTOMERS" table where the "SALARY" is equal to '2000'
SELECT * FROM CUSTOMERSWHERE NOT SALARY != '2000';
Output
When the query gets executed it will generate the following output as shown below
NOT EQUAL Operator Using a Client Program
Besides using MySQL queries to perform the NOT EQUAL operator, we can also use client programs like Node.js, PHP, Java, and Python to achieve the same result.
Syntax
Following are the syntaxes of this operation in various programming languages
PHPNodeJSJavaPython
To perform the NOT EQUAL Operator on a MySQL table through a PHP program, we need to execute SELECT statement with NOT EQUAL Operator using the mysqli function query() as follows
$sql = "SELECT column1, column2, ... FROM table_name WHERE column_name <> value";$mysqli->query($sql);
Example
Following are the programs
PHPNodeJSJavaPython
$dbhost = 'localhost';$dbuser = 'root';$dbpass = 'password';$dbname = 'TUTORIALS';$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);if($mysqli->connect_errno ) { printf("Connect failed: %s", $mysqli->connect_error); exit();}//printf('Connected successfully.');$sql = "SELECT * FROM CUSTOMERS WHERE NAME <> 'Muffy'";$result = $mysqli->query($sql);if ($result->num_rows > 0) { printf("Table records: \n"); while($row = $result->fetch_assoc()) { printf("Id %d, Name: %s, Age: %d, Address %s, Salary %f", $row["ID"], $row["NAME"], $row["AGE"], $row["ADDRESS"], $row["SALARY"]); printf("\n"); }} else { printf('No record found.');}mysqli_free_result($result);$mysqli->close();
Output
The output obtained is as follows
Table records:Id 1, Name: Ramesh, Age: 32, Address Hyderabad, Salary 4000.000000Id 2, Name: Khilan, Age: 25, Address Kerala, Salary 8000.000000Id 3, Name: kaushik, Age: 23, Address Hyderabad, Salary 11000.000000Id 4, Name: Chaital, Age: 25, Address Mumbai, Salary 1200.000000Id 5, Name: Hardik, Age: 27, Address Vishakapatnam, Salary 10000.000000Id 6, Name: Komal, Age: 29, Address Vishakapatnam, Salary 7000.000000