The Not Equal comparative operator is a very common operator used in T-SQL, however, it is important to understand how to use it effectively in different scenarios. A comparison operator in simple words compares two expressions and based on the comparison it tells us whether they are same or not. According to Microsoft documentation, an expression is a combination of symbols and operators that the SQL Server Database Engine evaluates to obtain a single data value.
There is another way to express Not Equal comparison operator and that is by using exclamation mark followed by equal sign! Please remember that the upcoming examples run against the sample database SQLDevBlogV2 which must be created if you would like to run the scripts of examples as mentioned in the pre-requisites. We have the Author table in the sample database which contains a list of all the registered authors. Please remember that we have already mentioned that the simplest form of expression is a number and in this example, we saw it in action.
If you refer to the sample database diagram at the beginning of this article, it is obvious that the Article table is connected with Category through foreign key relationship, so, first of all, we need to see the categories with articles by using INNER JOIN as follows:. A slightly complex scenario is when you are asked to get all the authors except those who were registered in the current year. If we run following SQL statement for greater than operator it will return records where empid greater than 2.
When we execute the above SQL greater than the operator query, we will get the result like as shown below. In SQL, less than operator is used to check whether the left-hand operator is lower than the right-hand operator or not. If left-hand operator lower than right-hand operator then condition will be true and it will return matched records. If we run following SQL statement for less than operator it will return records where empid less than 2. When we execute the above SQL less than operator query, we will get the result like as shown below.
In SQL, greater than or equal to the operator is used to check whether the left-hand operator is higher than or equal to the right-hand operator or not. If left-hand operator higher than or equal to right-hand operator then condition will be true and it will return matched records.
If we run following SQL statement for greater than or equal to the operator it will return records where empid higher than or equal to 2. When we execute the above SQL greater than or equal to operator query, we will get the result like as shown below.
Select Count ProductLaunchDate. Set Statistics Time On. Author Recent Posts. Rajendra Gupta. As an MCSA certified and Microsoft Certified Trainer in Gurgaon, India, with 13 years of experience, Rajendra works for a variety of large companies focusing on performance optimization, monitoring, high availability, and disaster recovery strategies and implementation.
He is the creator of one of the biggest free online collections of articles on a single topic, with his part series on SQL Server Always On Availability Groups. Raj is always interested in new challenges so if you need consulting help on any subject covered in his writings, he can be reached at rajendra.
Actually, this may depend on your DBMS and its configuration. Source Table Imagine our database contains the following table.
Strings When comparing with a string value, use quotes around the string. Therefore, we would need to rewrite the above statement as follows.
0コメント