Teradata Logical and Conditional Operators
Teradata supports the following logical and conditional operators. These operators are used to perform comparisons and combine multiple conditions.
Operator | Meaning |
---|---|
= | Equal to |
<> | Not Equal to |
> | Greater than |
< | Less than |
> = | Greater than or Equal to |
< = | Less than or Equal to |
AND | AND operator combines multiple conditions. Evaluates to true only if all the conditions are met. |
OR | OR operator combines multiple conditions. Evaluates to true if either of the condition is met. |
NOT | Reverse the meaning of the condition |
BETWEEN | If the values with range |
IN | If the values in <expression> |
NOT IN | If the values not in <expression> |
IS NULL | If the value is NULL |
BETWEEN Operator
BETWEEN operators are used to checking if a value is within a range of values.
Example
Consider the following student table.
RollNo | FirstName | LastName | BirthDate |
---|---|---|---|
1001 | Mike | Richard | 1/2/1996 |
1002 | Robert | Williams | 3/5/1995 |
1003 | Peter | Collin | 4/1/1994 |
1004 | Alexa | Stuart | 11/6/1995 |
1005 | Robert | Peterson | 12/1/1997 |
The following example fetches records with RollNo in the range between 1001, 1002, and 1003.
When the above query is executed, it returns the student records with RollNo between 1001 and 1003.
RollNo | FirstName |
---|---|
1001 | Mike |
1002 | Robert |
1003 | Peter |
IN Operator
IN operator is used to check the value against a given list of values.
Example
The following example fetches records with RollNo in 1001, and 1002.
The above query returns the following records.
RollNo | FirstName |
---|---|
1001 | Mike |
1002 | Robert |
NOT IN Operator
NOT IN command reverses the result of IN command. It fetches records with values that don’t match with the given list.
Example
The following example fetches records with employee numbers, not in 1001 and 1002.
The above query returns the following output, such as:
RollNo | FirstName | LastName |
---|---|---|
1003 | Peter | Collin |
1004 | Alexa | Stuart |
1005 | Robert | Peterson |
AND Operator
AND operator is used to connecting different LOGICAL operators. All the conditions must be met for a specific row to be qualified.
Example
The following example fetches the student record whose first name is Peter, and the last name is Collin.
Above query returns the following output, such as:
RollNo | FirstName | LastName | BirthDate |
---|---|---|---|
1003 | Peter | Collin | 4/1/1994 |
OR Operator
OR operator is used to connecting between different Logical operators. Only one of the conditions needs to be true for a row to be qualified.
Example
The following example fetches the students whose first name is Robert OR the last name is Peterson.
Above query returns the following output, such as:
RollNo | FirstName | LastName | BirthDate |
---|---|---|---|
1002 | Robert | Williams | 3/5/1995 |
1005 | Robert | Peterson | 12/1/1997 |