SQL Not Equal To
What is the SQL Not Equals To Operator?
The SQL "Not Equals To" operator, denoted as "<>", "!=", or "NOT =", is used to compare values in a database table and retrieve rows where a specific column's value does not match a given criteria.
When you would use it
You would use the "Not Equals To" operator when you want to exclude rows from a database table where a column's value does not match a particular value or expression. This is particularly useful when you need to filter out specific data that does not meet your criteria.
Syntax
The basic syntax for using the "Not Equals To" operator in an SQL query is as follows:
SELECT column1, column2
FROM table_name
WHERE column_name <> value;
SELECT
: Specifies the columns you want to retrieve.FROM
: Specifies the table from which you want to retrieve data.WHERE
: Specifies the condition for filtering rows.column_name
: The name of the column you want to compare.value
: The value or expression you want to compare the column with.
Parameter values
column_name
: The name of the column you want to filter.value
: The value or expression you want to compare against.
Example query
Suppose we have a table called "products" with columns: "product_id," "product_name," and "price." Here's an example query using the "Not Equals To" operator:
SELECT product_name, price
FROM products
WHERE price <> 10.99;
In this query, we're retrieving the product names and prices of products whose price
is not equal to $10.99.
Example table response
Assuming the "products" table has the following data:
product_id | product_name | price |
---|---|---|
1 | Widget A | 9.99 |
2 | Widget B | 12.99 |
3 | Widget C | 10.99 |
4 | Widget D | 8.99 |
The query would return:
product_name | price |
---|---|
Widget A | 9.99 |
Widget B | 12.99 |
Widget D | 8.99 |
Use cases
The "Not Equals To" operator is valuable in a variety of scenarios, including:
- Excluding records that do not meet specific criteria, such as filtering out items that are not on sale.
- Finding rows that have values that are not equal to a particular reference, like locating customers who haven't made a purchase.
- Identifying differences between tables when performing data comparisons.
SQL languages this is available for
The "Not Equals To" operator is a common feature supported by all major SQL database management systems (DBMS) and SQL-based languages, including but not limited to:
- MySQL
- PostgreSQL
- Oracle Database
- Microsoft SQL Server
- SQLite
- IBM Db2
In summary, the "Not Equals To" operator is a fundamental tool for filtering and excluding data in SQL databases, and it is supported in all major SQL database systems. It provides a means to specify conditions for excluding rows that do not match your criteria.