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 Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read