Skip to content
Related Articles

Related Articles

Improve Article

Inequality Operator in SQL

  • Last Updated : 09 Jul, 2021

In this article, we will discuss the overview of operators in SQL and mainly our focus will be on Inequality Operator in SQL. Let’s discuss it one by one.

Pre-requisite – Operators in SQL

Overview :
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 condition, the output will come. For example, if you want to find the data from the database let’s say student name then you can use an equal operator to check the name. 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 from the database. So, we will use an equal operator with 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 is a table name and name, year, subjects are the column’s name of the table.



students
nameyearsubjects
Anish Mandal4Compiler Design
Youbraj Rai4Java
Swatee Verma2Php

Example-1 :
So to 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 <> 2;

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

  • Like before, we start with selecting all items with *. This will give us students from all years.
  • Since year<>2 filters out student items with 2 in the year column, we only get a student from the fourth year.
     
result
nameyearsubjects
Anish Mandal4Compiler Design
Youbraj Rai4Java

 

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”, “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.

Result
nameyearsubjects
Anish4Compiler Design
Swatee Verma2Php

 

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

Note :

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

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

My Personal Notes arrow_drop_up
Recommended Articles
Page :