bytes

tutorials

sql

equal and not equal to in sql

Not Equal and Equal in SQL

Module - 7 Filtering in SQL
Not Equal and Equal in SQL

Overview

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 will return true when two expressions are not equal and false when they are equal. Equal will return true when two expressions are equal and untrue when they are not equal.

Not Equal

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 more about 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 the != operator. This 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.

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

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

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

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:
  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:
  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 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: 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. <>

Related Programs
Full Stack Data Science with Placement Guarantee of 5+ LPA
Course
20,000 people are doing this course
Become a job-ready Data Science professional in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Related Tutorials

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

avatar
Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter
Vikash SrivastavaCo-founder & CPTO AlmaBetter
Vikas CTO
Related Tutorials to watch
Top Articles toRead
AlmaBetter
Made with heartin Bengaluru, India
  • Location
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2022 AlmaBetter