Open In App

Inequality Operator in SQL

Pre-requisite – Operators in SQL

In this article, we will discuss the overview of operators in SQL, and mainly our focus will be on Inequality Operators in SQL. 



Let’s discuss them one by one.SQL Operators are widely used to access information on the basis of conditions. In the Inequality operator, we will check the condition on a specific column. Operator in SQL is used to perform conditions and on the basis of the condition, the output will come. For example, if you want to find the data from the database let’s say the student’s name then you can use an equal operator to check the name. The inequality operator is a reserved word used in an SQL statement WHERE clause to compare the two elements.

Example –



In this example, let’s suppose you want to access student names from the student table in the database. So, we will use an equal operator with a WHERE clause as follows

Select * from student where name = 'ABC';

Inequality Operator in SQL:

Sometimes when we want to select data that doesn’t satisfy a condition, like all students that are not in their 2nd year. 

Consider the given below table for reference. In this table, students are a table name and name, year, subjects are the column’s name of the table.

CREATE TABLE:

CREATE TABLE mytable (
    name VARCHAR(50),
    year INT,
    subject VARCHAR(50)
);
INSERT INTO mytable (name, year, subject)
VALUES ('John Doe', 2021, 'Math'),
       ('Jane Smith', 2022, 'English'),
       ('Bob Johnson', 2020, 'Science');

Output:

 

Example-1 :
So select those who are not in their 2nd year. So, In this, we will use the inequality operator as follows.

SELECT * FROM students WHERE year <> 2022;

Output :
There will be 2 records selected. These are the results you should see with either one of the SQL statements as follows.

 

Example-2 :
We can also use the inequality operator with text values, like getting all students that don’t subject in ‘Java’.

SELECT * FROM students WHERE subjects <> 'Java';

There will be 2 records selected i.e. “Compiler Design”, and “Php”. These are the results you should see with either one of the SQL statements. In the example, both SELECT statements would return all rows from the subjects table where the subjects are not equal to Java.

Output:

 

Use : 
To check if a column’s value is not equal to another, we use the inequality operator <>.

Difference between != and <> Operator

Technically saying that there is no difference between != and <>. Both of them actually work in the same way and there is absolutely no difference in terms of performance or result.

Frequently asked question

1. What kind of value can we use the inequality operator <> with?
Ans- Any kind of text and numbers.
2. When is the inequality sign useful?
Ans- When you want to get all items that don’t satisfy a criterion. 

Article Tags :
SQL