bytes

tutorials

sql

equal and not equal to in sql

Module - 7 Filtering in SQL

Lesson - 3 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_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'.

**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**

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.

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

- Utilize the not equal operator (“<>”) in an SQL statement to return records that are not equal to the required value.
- The “<>” operator checks for inequality between two values.
- The operator can be utilized to compare numbers, strings, or dates.
- When using the operator, use the proper syntax for the type of information you're comparing.

**Equal:**

- Utilize the equal operator (“=”) in an SQL statement to return records that rise to the desired esteem.
- The “=” operator is utilized to check for equality between two values.
- The administrator can be utilized to compare numbers, strings, or dates.
- When utilizing the operator, make, beyond any doubt to use the right syntax for the sort of information you're comparing.

**Quiz**

**Which of the following statements is true about the NOT EQUAL operator in SQL?**- It is used to check for equality between two values
- It is used to check for inequality between two values
- It is used to check for the similarity between two values
- It is used to check for differences between two values

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

**Which of the following statements is true about the EQUAL operator in SQL?**- It is used to check for equality between two values
- It is used to check for inequality between two values
- It is used to check for the similarity between two values
- It is used to check for differences between two values

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

**What is the difference between the NOT EQUAL and EQUAL operators in SQL?**- Not Equal is used to check for equality, and Equal is used to check for inequality
- Not Equal is used to check for inequality, and Equal is used to check for equality
- Not Equal is used to check for similarity, and Equal is used to check for the difference
- 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

**What comparison operator is used to check for inequality between two values in SQL?**- <>
- =
- !=
- ==

**Answer**: a. <>

Made with

in Bengaluru, India - Join AlmaBetter
- Sign Up
- Become A Coach
- Coach Login

- Contact Us
- admissions@almabetter.com
- 08046008400

- Location
- 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025

- Follow Us

© 2022 AlmaBetter