Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PL/SQL NOT EQUAL Operator
Next article icon

MySQL NOT EQUAL Operator

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • Using the != operator
  • Using the <> 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 table
Employees Table

1. 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:

idnamedepartmentsalary
1John DoeHR50000
4Lucy BlackFinance80000
5Mark WhiteHR55000

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:

idnamedepartmentsalary
1John DoeHR50000
3Sam BrownIT70000
4Lucy BlackFinance80000
5Mark WhiteHR55000

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.


Next Article
PL/SQL NOT EQUAL Operator
author
tmpravi5i5j
Improve
Article Tags :
  • Databases
  • MySQL

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences