Microsoft SQL Server
'< >' or '!=' Operator (not equal)
In Microsoft SQL Server, the <> and != operators are used to filter data where the values in a column are not equal to a specified value. These operators serve the same purpose and can be used interchangeably to exclude certain values from your results. For beginners, learning how to use <> and != effectively is crucial for writing queries that need to filter out specific data or exclude particular records from your result set.
Below is a detailed explanation of how to use the <> or != operators with examples and best practices.
1. Basic Syntax of <> or != (Not Equal)
The <> and != operators are used to filter rows where the value in the column is not equal to a specific value.
SELECT column_name FROM table_name WHERE column_name <> value;
- Replace
column_namewith the column you want to compare. - Replace
table_namewith the actual table name. - Replace
valuewith the number, text, or date to compare against.
Example:
SELECT ProductName, Price FROM Products WHERE Category <> 'Electronics';
This query retrieves all products that do not belong to the Electronics category.
2. Using <> or != with Numeric Values
You can use <> or != with numeric values to filter out rows that match a particular number.
SELECT CustomerName, Country FROM Customers WHERE CustomerID <> 1;
This query retrieves all customers except the one with CustomerID equal to 1.
3. Using <> or != with Dates
These operators can also be used to filter records where a column's date value is not equal to a specified date.
SELECT OrderID, OrderDate FROM Sales WHERE OrderDate <> '2023-01-01';
This query retrieves all sales orders that were not placed on January 1, 2023.
4. Using <> or != with Multiple Conditions
You can combine <> or != with other SQL conditions like AND or OR to further refine your search.
SELECT ProductName, Price FROM Products WHERE Category <> 'Electronics' AND Price > 100;
This query retrieves all products that do not belong to the Electronics category and have a price greater than 100.
5. Handling NULL Values with <> or !=
Be aware that <> or != does not return rows where the value is NULL. In SQL, NULL is treated as an unknown value, so it is neither equal to nor not equal to any value. To include NULL in your condition, you need to explicitly check for it.
SELECT ProductName FROM Products WHERE Category <> 'Electronics' OR Category IS NULL;
This query retrieves all products that do not belong to the Electronics category, as well as those that have no category (NULL).
6. Best Practices for Using <> or !=
Use
<>and!=Interchangeably – In SQL Server, both<>and!=are functionally the same. You can use whichever you prefer, but for consistency, it’s a good practice to stick with one operator throughout your queries.SELECT * FROM Customers WHERE Country != 'USA';Handle
NULLValues Explicitly – Remember thatNULLvalues are not included in<>or!=comparisons. Always include anIS NULLcondition if you want to capture records where the value might be missing.SELECT * FROM Products WHERE Category <> 'Clothing' OR Category IS NULL;Combine with
ANDandORfor Complex Conditions – You can use<>or!=along withANDorORto create more complex queries that refine your search.SELECT * FROM Products WHERE Price != 50 AND StockQuantity > 0;Use Indexing for Performance – When frequently using
<>or!=on large tables, ensure that the column you're filtering on is indexed to improve query performance.
By using the <> or != operators effectively, you can exclude specific values and refine your queries to retrieve only the relevant data, making your SQL queries more precise and efficient.
To gain complete access, login with gmail or outlook, no need of signup, click here
Test code
In Microsoft SQL Server, you would use the <> operator to filter out female clients:
SELECT *
FROM org_client
WHERE gender <> 'F';In Microsoft SQL Server, the <> operator serves the same purpose for "not equal" comparisons:
SELECT *
FROM act_order
WHERE order_status_id <> 5;In Microsoft SQL Server, you can use the <> operator for "not equal" comparisons:
SELECT *
FROM act_order
WHERE shipped_date <> '2023-12-12';Example 1:
Let's examine the process of extracting information from a specified table using the SQL '!=' or '<>' (not equal) operator with string data. Retrieve records for clients labeled as male while excluding those identified as female.
Example 1 - Raw data from client table

Example 1 - Query
SELECT * FROM org_client WHERE gender <> 'F';Example 1 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 1 - Query Output

Example 2:
Let's explore the procedure of extracting information from a designated table using the SQL '!=' or '<>' (not equal) operator with numeric data. Fetch orders with statuses different from 5.
Example 2 - Raw data from orders table

Example 2 - Query
SELECT * FROM act_order WHERE order_status_id != 5;Example 2 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 2 - Query Output

Example 3:
Let's delve into the process of extracting information from a specified table using the SQL '!=' or '<>' (not equal) operator with date data. Retrieve orders with shipping dates other than December 12th.
Example 3 - Raw data from orders table

Example 3 - Query
SELECT * FROM act_order WHERE shipped_date != '2023-12-12';Example 3 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query. Please be aware that the query will attempt to filter based on the value of December 12th. Since many records have NULL for the shipped date value, NULL cannot be compared as it is not an actual value. Consequently, these records will be excluded from the results as well.
Example 3 - Query Output



Comments Not Found