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.
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.
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.
customers
customer_name | phone_number | address |
---|---|---|
Jane Doe | 555-123-4567 | 123 Main St |
Joe Johnson | 555-987-6543 | 456 Wall St |
Mary Jones | 555-222-0984 | 789 Center Ave |
Steve Rogers | 555-111-7777 | 987 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'.
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.
Customer
id | name | age |
---|---|---|
1 | John | 28 |
2 | Steve | 28 |
SELECT *
FROM customers
WHERE age = 28;
This example returns all rows from the customer's table where the age column value equals 28.
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!
Answer: b. It is used to check for inequality between two values
Answer: a. It is used to check for equality between two values
Answer: B. Not Equal is used to check for inequality, and Equal is used to check for equality
Answer: a. <>
Top Tutorials
Related Articles