Bytes

Equal and Not Equal to in SQL

The Not Equal ( != or <>) and Equal ( = ) operators are utilized in SQL to compare two expressions and decide whether they are equal or not. Not Equal in SQL will return true when two expressions are not equal and false when they are equal. Equal in SQL will return true when two expressions are equal and untrue when they are not equal.

Not Equal in SQL

A computer programmer named Priya. She was always looking for ways to optimize her code and make it run faster and more efficiently. One day, she decided to learn SQL and how to use it to her advantage. She quickly learned the basics of SQL and was excited to start using it. She soon noticed, however, that it was difficult to compare two values in her code. She tried using the "Equal" and "Not Equal" operators but wasn't getting the desired result. Let's help her understand it better.

The not equal operator in SQL is != (not equal to sign in SQL). This SQL operator compares two expressions and determines if they are not equal. The result of a comparison using the != operator will be either true or false. Let’s look at the syntax of how to write not equal to in SQL.

Syntax:

SELECT * 
FROM [table_name] 
WHERE [column_name] != [value]

This can be used to compare any column in any table to any value. The result of the comparison will be true if the column does not equal the value and false if the column does equal the value.

Example of Not Equal in SQL

customers

customer_namephone_numberaddress
Jane Doe555-123-4567123 Main St
Joe Johnson555-987-6543456 Wall St
Mary Jones555-222-0984789 Center Ave
Steve Rogers555-111-7777987 First Ave
SELECT *
FROM customers
WHERE customer_name != 'John Smith'

This example will select all customers from the customer's table, except for customers named 'John Smith'.

Equal in SQL

The equal operator (=) in SQL is a comparison operator used to test for equality between two expressions. It is used to compare one expression's value to another's value. If the two expressions are equal, the condition evaluates to true, otherwise, the condition evaluates to false.

`
SELECT * 
FROM [table_name] 
WHERE [column_name] = [value]

This SQL statement selects all records from the specified table where the value in the specified column equals the specified value.

Example of Equal in SQL

Customer

idnameage
1John28
2Steve28
SELECT *
FROM customers
WHERE age = 28;

This example returns all rows from the customer's table where the age column value equals 28.

Conclusion

She realized that in the event that the two values weren't the same but were near, the "Not Equal" operator would be vital. She has chosen to utilize the "Not Equal" operator to ensure her code was running accurately. By utilizing the "Not Equal" and "Equal" operators in her code, Priya could form her code run much more productively. She was so energized to finally figure out how to compare two values in her code!

Key Takeaways

  • Not Equal in SQL:
  1. Utilize the not equal operator (“<>”) in an SQL statement to return records that are not equal to the required value.
  2. The “<>” operator checks for inequality between two values.
  3. The operator can be utilized to compare numbers, strings, or dates.
  4. When using the operator, use the proper syntax for the type of information you're comparing.
  • Equal in SQL:
  1. Utilize the equal operator (“=”) in an SQL statement to return records that rise to the desired esteem.
  2. The “=” operator is utilized to check for equality between two values.
  3. The administrator can be utilized to compare numbers, strings, or dates.
  4. When utilizing the operator, make, beyond any doubt to use the right syntax for the sort of information you're comparing.

Quiz

  1. Which of the following statements is true about the NOT EQUAL in SQL?
    1. It is used to check for equality between two values 
    2. It is used to check for inequality between two values  
    3. It is used to check for the similarity between two values  
    4. It is used to check for differences between two values

Answer: b. It is used to check for inequality between two values

  1. Which of the following statements is true about the EQUAL operator in SQL?  
    1. It is used to check for equality between two values 
    2. It is used to check for inequality between two values  
    3. It is used to check for the similarity between two values 
    4. It is used to check for differences between two values

Answer: a. It is used to check for equality between two values 

  1. What is the difference between the NOT EQUAL and EQUAL operators in SQL?
    1. Not Equal is used to check for equality, and Equal is used to check for inequality 
    2. Not Equal is used to check for inequality, and Equal is used to check for equality 
    3. Not Equal is used to check for similarity, and Equal is used to check for the difference 
    4. Not Equal is used to check for the difference and Equal is used to check for similarity

Answer: B. Not Equal is used to check for inequality, and Equal is used to check for equality

  1. What comparison operator is used to check for inequality between two values in SQL? 
    1. <>  
    2. =  
    3. !=  
    4. ==

Answer: a. <>

Module 7: Filtering in SQLEqual and Not Equal to in SQL

Top Tutorials

Related Articles

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter