SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. It provides a standardized language for querying the databases which are found to be structured, allowing users to define, manipulate, and control the data retrieval with ease. SQL operates through a variety of commands and operators that facilitate some of the most important tasks that are listed below:
- Data retrieval
- Insertion
- Updating
- Deletion
To perform these operations, learning about the operators that are used in them is crucial. Having an in-depth knowledge of the operators and their syntax allows us to write much more efficient queries.
MySQL NOT EQUAL Operator
In MySQL, the NOT EQUAL operator is used to compare two expressions to determine if they are not equal. This operator is primarily used for filtering results in queries to exclude specific values or find records that differ from a given value. Efficient use of the NOT EQUAL operator can significantly enhance your ability to manipulate and retrieve data from a MySQL database.
In MySQL, there are two ways to express the NOT EQUAL operator:
Both operators function identically and can be used interchangeably.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name != value;
-- or
SELECT column_name(s)
FROM table_name
WHERE column_name <> value;
Parameters:
- column_name is the column you want to compare.
- value is the value you want to compare against.
Let's understand the working of a NOT EQUAL operator by working with a live example.
Example of MySQL NOT EQUAL Operator
Creatin the employees table:
-- Create the employees table
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary INT
);
-- Insert data into the employees table
INSERT INTO employees (id, name, department, salary) VALUES
(1, 'John Doe', 'HR', 50000),
(2, 'Jane Smith', 'IT', 60000),
(3, 'Sam Brown', 'IT', 70000),
(4, 'Lucy Black', 'Finance', 80000),
(5, 'Mark White', 'HR', 55000);
Output:
Employees Table1. Using != Operator
To find all employees who do not work in the IT department, you can use the following query:
SELECT name, department
FROM employees
WHERE department != 'IT';
Output:
id | name | department | salary |
---|
1 | John Doe | HR | 50000 |
4 | Lucy Black | Finance | 80000 |
5 | Mark White | HR | 55000 |
Explanation: This query retrieves the names and departments of all employees who do not work in the IT department from the employees table. It filters out employees in the IT department, returning only those in other departments.
2. Using the <> operator
Suppose you want to find all employees who do not have a salary of 60000. You can use the following query
SELECT name, salary
FROM employees
WHERE salary <> 60000;
Output:
id | name | department | salary |
---|
1 | John Doe | HR | 50000 |
3 | Sam Brown | IT | 70000 |
4 | Lucy Black | Finance | 80000 |
5 | Mark White | HR | 55000 |
Explanation: This query retrieves the names and salaries of all employees who do not have a salary of 60,000 from the employees table. It filters out employees with a salary of 60,000, returning only those with different salaries.
Conclusion
The MySQL NOT EQUAL operator (!= or <>) is a powerful and flexible tool for filtering data that doesn't match a specific value. By learning how to use this operator effectively, you can make your SQL queries more precise and efficient. Whether you're excluding certain records or searching for values that are different from a given criterion, the NOT EQUAL operator can help you get the results you need. Adding this operator to your SQL skill set will enhance your ability to work with databases and retrieve the exact data you want.
Similar Reads
SQL NOT EQUAL Operator
The SQL NOT EQUAL operator is a comparison operator used to check if two expressions are not equal to each other. It helps filter out records that match certain conditions, making it a valuable tool in SQL queries. In this article, We will explore the SQL NOT EQUAL operator, including its syntax, us
4 min read
MySQL IS NULL Operator
The IS NULL operator in MySQL is a powerful tool for handling records with missing or incomplete data. It enables precise querying and data management by allowing users to identify and act upon fields where values are absent. In this article, We will learn about the MySQL IS NULL Operator by underst
3 min read
MySQL LIKE Operator
The MySQL LIKE operator helps us search for specified patterns in a column. It is useful when we need to find records that match specific patterns, like names starting with a certain letter or containing a specific word. In this article, we will cover the concept, syntax, and examples of using the L
3 min read
PL/SQL NOT EQUAL Operator
In PL/SQL, the NOT EQUAL operator is used to compare two values and determine if they are not equal. If the values are different, the result of the comparison is true; otherwise, it is false. This operator is often used in conditional statements and queries to filter data based on inequality. In thi
6 min read
Python NOT EQUAL operator
In this article, we are going to see != (Not equal) operators. In Python, != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Python NOT EQUAL operators SyntaxThe Operator not equal in the Python descrip
3 min read
MySQL IN Operator
The MySQL IN operator is used to filter data based on a specified set of values. It is a shorthand for multiple OR conditions which allows us to specify a list of values in a WHERE clause to match records that have any of those values. This makes your SQL queries more concise and easier to read. MyS
3 min read
MySQL EXCEPT Operator
Comparison of two different sets of data in a database and finding entries unique to one set is one of the common operations done on databases. The EXCEPT operator in SQL compares the two result sets returned and returns only the rows that are found in one but not the other. Moreover, MySQL, one of
6 min read
MySQL EXISTS Operator
The EXISTS operator in MySQL is a powerful boolean operator used to test the existence of any record in a subquery. It returns true if the subquery yields one or more records, enabling efficient data retrieval and manipulation, particularly in large datasets. The operator is often paired with subque
6 min read
MongoDB - $eq Operator
MongoDB provides a variety of comparison query operators to filter and retrieve documents efficiently. One of the most widely used operators is $eq (equal to operator), which allows users to match exact values in a MongoDB collection. In this article, we will explore the MongoDB $eq operator, its sy
4 min read
MySQL BETWEEN Operator
MySQL consists of various operators for performing efficient data queries, and the BETWEEN operator is a key operator for filtering records within a specific range. By specifying a lower and upper bound, BETWEEN helps you easily retrieve data that falls between two values. This operator simplifies q
4 min read