SQL Tutorial SQL Advanced SQL Database SQL References

SQL - Not equal to (!=) Operator



The SQL != (not equal to) operator checks if the value of left operand is not equal to the value of right operand and returns true if the condition is true, false otherwise.

The example below describes how to use this operator in various conditions:

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • Using with WHERE Clause: To select records of table where Age of the person is not equal to 28, the SQL code is given below.

    SELECT * FROM Employee
    WHERE Age != 28;
    

    The query will produce following result:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    2MarryNew York242750
    3JoParis272800
    4KimAmsterdam303100
  • Using with AS Clause: If the above condition is used with AS clause, it will produce a column having boolean result, 1 if the condition is true and 0 if the condition is false. See the example below:

    SELECT *, (Age != 28) AS Result FROM Employee;
    

    The above query will produce following result:

    EmpIDNameCityAgeSalaryResult
    1JohnLondon2530001
    2MarryNew York2427501
    3JoParis2728001
    4KimAmsterdam3031001
    5RameshNew Delhi2830000
    6HuangBeijing2828000
  • Using with UPDATE Clause: The operator can also be used to update a column with boolean result, 1 if the condition is true and 0 if the condition is false. See the example query below:

    UPDATE Employee
    SET Age = (Age != 28);
    
    --See result
    SELECT * FROM Employee
    

    The query will produce following result:

    EmpIDNameCityAgeSalary
    1JohnLondon13000
    2MarryNew York12750
    3JoParis12800
    4KimAmsterdam13100
    5RameshNew Delhi03000
    6HuangBeijing02800
  • Using with values: A condition can be checked simply by using SELECT statement:

    SELECT 50 != 25
    

    The query will produce following result:

    50 != 25
    1

❮ SQL Operators

5