Inequality Operator in SQL
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 | ||
---|---|---|
name | year | subjects |
Anish Mandal | 4 | Compiler Design |
Youbraj Rai | 4 | Java |
Swatee Verma | 2 | Php |
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 | ||
---|---|---|
name | year | subjects |
Anish Mandal | 4 | Compiler Design |
Youbraj Rai | 4 | Java |
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 | ||
---|---|---|
name | year | subjects |
Anish | 4 | Compiler Design |
Swatee Verma | 2 | Php |
Use :
To check if a column’s value is not equal to another, we use the inequality operator <>.
Note :
- What kind of value can we use the inequality operator <> with?
Ans- Any kind like text and numbers.
- When is the inequality sign useful ?
Ans- When you want to get all items that don’t satisfy a criterion.
Please Login to comment...