Today’s post focuses on the MySQL “Not Equal” clause. We will discuss how it works by giving various examples on how to apply it to your query. Let’s dig in!
Working with the MySQL “Not Equal” Clause
With MySQL, you have two ways of implementing the “Not Equal” filter. You can use the standard working method with the <> symbol or the “!=” operator. Both options yield the same results, only that the “<>” adheres to the ISO standard while the “!=” operator doesn’t.
You can perform your MySQL inequality test with either of the following syntaxes:
or
SELECT <statement> FROM table_name WHERE column_name != ‘value’;
This tutorial uses the “details” table to give examples of using the “Not Equal” clause. Our table contains three columns as shown in the following:
How you apply the “Not Equal” clause depends on your selected goal. We give different examples to apply the “Not Equal” clause.
For our first example, let’s try how to select all the values where the age is not equal to 27. In this case, we get an output that shows any data where the age is not equal to 27. We run the command as follows:
Note how there is no output that shows the age as 27 in the following output because we choose to exclude it in our select query.
You can also achieve the same results using the “!=” operator. In that case, only the “Not Equal” operator will change. Our new statement will be as follows:
We still get the same output.
Suppose we want to filter a string column. The syntax remains the same, only that you must enclose the value with quotes to specify that we are dealing with a string value. Here’s an example where we want to select all values that don’t have their course as “IT”.
We execute our command as follows:
The select query gives nine rows and excludes the one where the course is what we filtered in our query.
We can use the AND clause to combine two columns in our query if we want a more specific result. For instance, let’s say we want to filter where the age is greater than 27 and where the course is not equal to “IT”.
Our command is as follows:
Based on our select query, we get three rows in the result.
Additionally, you can specify what columns in your table to select when executing the “Not Equal” clause. If we were to execute a similar command to the previous one and specify only to select the name and age columns and change the course, we could have our new command as shown in the following:
As in the following image, the same command can be executed with the “!=” equal operator. You still get the same output.
Conclusion
The MySQL “Not Equal” clause lets you filter the results when executing the select query. Throughout this post, we detailed how you can apply the “Not Equal” clause by giving various examples. With that, try the same and implement the MySQL “Not Equal” clause in your select queries.